C Array of Structures

Consider a case, where we need to store the data of 5 students. We can store it by using the structure as given below.

  1. #include<stdio.h>  
  2. struct student  
  3. {  
  4.     char name[20];  
  5.     int id;  
  6.     float marks;  
  7. };  
  8. void main()  
  9. {  
  10.     struct student s1,s2,s3;  
  11.     int dummy;  
  12.     printf("Enter the name, id, and marks of student 1 ");  
  13.     scanf("%s %d %f",s1.name,&s1.id,&s1.marks);  
  14.     scanf("%c",&dummy);  
  15.     printf("Enter the name, id, and marks of student 2 ");  
  16.     scanf("%s %d %f",s2.name,&s2.id,&s2.marks);  
  17.     scanf("%c",&dummy);  
  18.     printf("Enter the name, id, and marks of student 3 ");  
  19.     scanf("%s %d %f",s3.name,&s3.id,&s3.marks);  
  20.     scanf("%c",&dummy);  
  21.     printf("Printing the details....\n");  
  22.     printf("%s %d %f\n",s1.name,s1.id,s1.marks);  
  23.     printf("%s %d %f\n",s2.name,s2.id,s2.marks);  
  24.     printf("%s %d %f\n",s3.name,s3.id,s3.marks);  
  25. }  

Output

Enter the name, id, and marks of student 1 James 90 90  
Enter the name, id, and marks of student 2 Adoms 90 90  
Enter the name, id, and marks of student 3 Nick 90 90       
Printing the details....        
James 90 90.000000                          
Adoms 90 90.000000                      
Nick 90 90.000000 
  1. #include<stdio.h>  
  2. #include <string.h>    
  3. struct student{    
  4. int rollno;    
  5. char name[10];    
  6. };    
  7. int main(){    
  8. int i;    
  9. struct student st[5];    
  10. printf("Enter Records of 5 students");    
  11. for(i=0;i<5;i++){    
  12. printf("\nEnter Rollno:");    
  13. scanf("%d",&st[i].rollno);    
  14. printf("\nEnter Name:");    
  15. scanf("%s",&st[i].name);    
  16. }    
  17. printf("\nStudent Information List:");    
  18. for(i=0;i<5;i++){    
  19. printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);    
  20. }    
  21.    return 0;    
  22. }    

Output:

Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz

Student Information List:
Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz