Array of structures in C
#include
#include
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
record[0].id=1;
strcpy(record[0].name, "Raju");
record[0].percentage = 86.5;
record[1].id=2;
strcpy(record[1].name, "Surendren");
record[1].percentage = 90.5;
record[2].id=3;
strcpy(record[2].name, "Thiyagu");
record[2].percentage = 81.5;
for(i=0; i<3 i="" span="">3>
{
printf(" Records of Student : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;
}
Output
Records of Student : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000
Id is: 1
Name is: Raju
Percentage is: 86.500000
Records of Student : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of Student : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000
Program for declaring many structure variable in C
#include
#include
#include
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student record2 = {2, "Mani", 93.5};
printf("Records of Student1: \n");
printf(" Id is: %d \n", record1.id);
printf(" Name is: %s \n", record1.name);
printf(" Percentage is: %f \n\n", record1.percentage);
printf("Records of Student2: \n");
printf(" Id is: %d \n", record2.id);
printf(" Name is: %s \n", record2.name);
printf(" Percentage is: %f \n\n", record2.percentage);
return 0;
}
Output
Records of Student1:
Id is: 1
Name is: Raju
Percentage is: 90.500000
Records of Student2:
Id is: 2
Name is: Mani
Percentage is: 93.500000
Id is: 1
Name is: Raju
Percentage is: 90.500000
Records of Student2:
Id is: 2
Name is: Mani
Percentage is: 93.500000
No comments:
Post a Comment