logo of SA Coder
sign in



In C programming language, structures are a keyword used to create custom (user-defined) data types. Structures are a powerful feature of C programming language that allows the programmer to define a new data type with its own set of properties, similar to how we define primitive data types like int or float.

A structure is a collection of related data items, also known as members. These members can be of any data type, including primitive data types, arrays, and even other structures. In C programming language, we define a structure using the keyword 'struct'. The structure definition is followed by a name of the structure and a set of curly braces {} that enclose the list of members.

Here's an example of how we can define a structure in C programming language:


struct student 
    // this is called structure member
    char name[10];
    int roll;
    int id_number;
    float marks;

In this example, we have defined a structure named 'student'. The 'student' structure has four members: 'name', 'roll', 'id_number', and 'marks'. The name member is of type character array, and the other members are of primitive data types.

We can create variables of this structure type and access the members using the dot (.) operator. Here's an example:


struct student student1, student2; 
student1.roll = 1; 
student2.roll = 2;
student1.id_number = 111;
student2.id_number = 112;
student1.marks = 55.6;
student2.marks = 66.5;

printf(" Name = %s\n Roll = %d\n ID = %d \n Marks = %f\n",student1.name,student1.roll,student1.id_number,student1.marks);
printf(" Name = %s\n Roll = %d\n ID = %d \n Marks = %f\n",student2.name,student2.roll,student2.id_number,student2.marks);

In this example, we have created two variables of the 'student' structure type named 'student1' and 'student2'. We have assigned values to the members of these variables using the dot (.) operator. Finally, we have printed the values of these members using the printf() function.

In addition to creating variables of a structure type, we can also create an array of variables of the structure type. Here's an example:


struct student student_data[2];

for (int i = 0; i < 2; i++)
    printf("Enter some data of student(%d)\n Name Roll ID and Marks\n", i + 1);
    scanf("%s %d %d %f", &student_data[i].name, &student_data[i].roll, &student_data[i].id_number, &student_data[i].marks);

for (int i = 0; i < 2; i++)
    printf("some data of student(%d)\n", i + 1);
    printf(" Name = %s\n Roll = %d\n ID = %d \n Marks = %f\n", student_data[i].name, student_data[i].roll, student_data[i].id_number, student_data[i].marks);

In this example, the program is using a structure named "student" to create a user-defined data type. The program then declares an array of type "student" named "student_data" with a size of 2.

The program then uses a loop to prompt the user to enter data for each of the two students in the array. The scanf function is used to read in the data for each student's name, roll, ID, and marks. The data entered by the user is stored in the corresponding structure member for each student in the array.

Finally, the program uses another loop to display the data entered for each student. The printf function is used to display the data stored in each structure member for each student in the array.

Please login first to comment.