C Program for Students Details Using Structure
Write a C program to create a structure named student and print the name, age, and gender of two students (two structure objects)?
What is Structure?
The structure is a user-defined data type in C. Structure allows you to combine data items of different kinds. It is used to represent a record. Suppose you want to store a record of Student which consists of student name, address, roll number and age.
This program stores the information (name, age and gender) of students using structures.
#include<stdio.h>
int main()
{
struct stud
{
int age;
char name[20];
char gender[10];
};
struct stud *a;
struct stud a1;
printf(“Enter the name\n:”);
scanf(“%s”,&a1.name[0]);
printf(“Enter the age:”);
scanf(“%d”,&a1.age);
printf(“Enter the gender:\n”);
scanf(“%s”,&a1.gender[0]);
struct stud a2;
printf(“Enter the name\n:”);
scanf(“%s”,&a2.name[0]);
printf(“Enter the age:”);
scanf(“%d”,&a2.age);
printf(“Enter the gender:\n”);
scanf(“%s”,&a2.gender[0]);
a=&a1;
printf(“%s\n”,a->name);
printf(“%d\n”,a->age);
printf(“%s\n”,a->gender);
a=&a2;
printf(“%s\n”,a->name);
printf(“%d\n”,a->age);
printf(“%s\n”,a->gender);
}
Stay Connected with us for more Tutorial on C Programming Language. See here for more C Programming Language