Structures :

What is Structure ?

A structure is a user-defined compound data type which consists of data members related to a person or an item. It is referred to record in other programming languages.

Syntax :

  1. struct student
  2. {
  3. int roll_no;
  4. char sname[30];
  5. int total;
  6. };

Note : The structure definition ends with a semicolon ( ; ) after the closing brace.

Example :

  1. /* Description: structure program to store students data */
  2. #include<stdio.h>
  3. struct student{
  4. int roll_no;
  5. char sname[20];
  6. int total;
  7. }
  8. main()
  9. {
  10. struct student std[10];
  11. int i,j,k;
  12. puts("How many students data you want to store?(max 9) :");
  13. scanf("%d",&j);
  14. for(i=1;i<=j;i++)
  15. {
  16. printf("\nEnter No.%d student roll number : ",i);
  17. scanf("%d",&std[i].roll_no);
  18. printf("Enter No.%d student name : ",i);
  19. scanf("%s",&std[i].sname);
  20. printf("Enter No.%d student total number : ",i);
  21. scanf("%d",&std[i].total);
  22. }
  23. puts("\n------ DATA STORE COMPLETED ------");
  24. for(i=1;i<=j;i++)
  25. {
  26. printf("\n\nStudent No.%d student roll number : %d",i,std[i].roll_no);
  27. printf("\nStudent No.%d student roll number : %s",i,std[i].sname);
  28. printf("\nStudent No.%d student total number : %d",i,std[i].total);
  29. }
  30. getch();
  31. }

Output :

c language - structure program to store students data

Structure Variables and Arrays :

Structure variables can be declared using the tag-name of the structure along with the keyword struct.

Syntax :

  1. struct student
  2. {
  3. int roll_no;
  4. char sname[30];
  5. int total;
  6. };
  7. struct student x,y,z;
  8. struct student std[50];

It declares structure variables x, y and z and an array std with 50 elements.


Dot ( . ) Operator :

Dot (.) or period operator is used to access a data member in a structure variable or an array element. It has the following form :

structure_variables.data_member

Note : That the dot operator separates the structure variables or array and the data member.

Example :

x.roll_no - std[i].roll_no
x.sname - std[i].sname
x.total - std[i].total


sizeof() Of a structure :

sizeof() of a structure is equal to the total number of bytes occupied in RAM by various data members in the structure.

  1. struct student
  2. {
  3. int roll_no;
  4. char sname[30];
  5. int total;
  6. };

The size of this structure is equal to 24bytes ( 2bytes for int + 20bytes for char + 2bytes for int ). The sizeof() function is written as follows to print the size of the structure :

printf("%d",sizeof(struct student));

Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels