What is the structure in C Language?

  • The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.
  • The structure members can be accessed only through structure variables.
  • Structure variables accessing the same structure but the memory allocated for each variable will be different.

Syntax of structure

  1. struct structure_name
  2. {
  3.   Member_variable1;
  4.  Member_variable2
  5. .
  6. .
  7. }[structure variables];

Let’s see a simple example.

  1. #include <stdio.h>
  2. struct student
  3. {
  4.     char name[10];       // structure members declaration.
  5.     int age;
  6. }s1;      //structure variable
  7. int main()
  8. {
  9.     printf(“Enter the name”);
  10.     scanf(“%s”,s1.name);
  11.     printf(“\n”);
  12.     printf(“Enter the age”);
  13.     scanf(“%d”,&s1.age);
  14.     printf(“\n”);
  15.     printf(“Name and age of a student: %s,%d”,s1.name,s1.age);
  16.     return 0;
  17. }

Output:

Enter the name shikha
Enter the age 26
Name and age of a student: shikha,26