Structures in C

Structures in C

In this part you will learn: 1. C syntax 2. Structure variable 3. Structure declaration 4. Structure accessing 5. Showing output

What is a Structure?

Structure is a user-defined data type in C which allows you to combine different data types to store a particular type of record. Structure helps to construct a complex data type in a more meaningful way. Structure helps when you have to store a collection of data of many similar things or people etc. It is somewhat similar to an array. The difference is that an array is used to store a collection of similar type of data while structure can store collection of any type. Structure are best used to represent a record. If you want to store a record of hundred student you will need to define hundred variables to store the name and similarly hundred variables for each type of data you need to enter in the record. This is where structures come in handy. In this program we will make a structure of student that will store the name of the student, the age of the student and the gpa. Student Structure Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>
These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. struct Student
  2. {
  3. char name[50];
  4. int age;
  5. float gpa;
  6. };
We define structure before the main function. We write the struct keyword and the name of the structure. You can give any name to the structure except any keyword. Then in body of the structure we declare a char array of size 50 for storing the name the student. The and integer for storing the age and a float variable for the gpa. We close the brackets and place a semi colon at the end. Now when ever we will make a variable of student type all these three variables will be created in the memory.
  1. int main()
  2. {
  3.  
  4. Student s[3];
  5.  
  6. int i,j;
  7.  
  8. for(i=0; i<3;i++)
  9. {
  10. printf("Enter the name of the student %d: ",i+1);
  11. scanf(" %s",&s[i].name);
  12.  
  13. printf("Enter the age of student %d:",i+1);
  14. scanf("%d",&s[i].age);
  15.  
  16. printf("Enter the GPA of student %d:",i+1);
  17. scanf("%f",&s[i].gpa);
  18.  
  19. printf("\n");
  20.  
  21. }
  22.  
  23.  
  24. float maxgpa;
  25. int l;
  26.  
  27. for(l=0;l<3;l++)
  28. {
  29. if(l==0)
  30. maxgpa=s[l].gpa;
  31. else if(maxgpa<s[l].gpa)
  32. {
  33. maxgpa=s[l].gpa;
  34. }
  35. }
  36.  
Now we started the main function. In the main function we created a student array of size 3. That means we created three students. Then we started a for loop and took input in the array. Now every index of the array contains three types of data i.e. name of the student, age of the student and the gpa of the student. When taking input in the name of the student we just placed %s as format specifier and accessed the name variable with the ‘.’ Operator with the index. In this way we access any element of the structure. In the same way we took the input the other variables and the other students. After taking input in all the students we went for calculating the maximum gpa of the student which we did by using a for loop and comparing the gpas of students with eachother and stored the maximum gpa in another variable.
  1. printf("\nThe data you enetered.\n");
  2.  
  3. int m;
  4. for(m=0; m<3;m++)
  5. {
  6. printf("\nName:%s",s[m].name);
  7.  
  8. printf("\nAge:%d",s[m].age);
  9.  
  10. printf("\nGPA: %0.2f",s[m].gpa);
  11. printf("\n");
  12.  
  13.  
  14. }
  15.  
  16. printf("The Maximum GPA is:%0.2f",maxgpa);
  17.  
  18.  
  19. getch();
  20. }
After this we again implemented a for loop to display all the record of the students one by one. Then at last we displayed the maximum gpa of the students. And at the end we paused the program. Execute > compile then Execute > run Output picture viewer

Add new comment