Prerequisites:
Using the structure pointer, we will implement a C program to create and print a database of students. In the below program, a structure student is created. The structure has five members:
- first_Name (string)
- last_Name (string)
- roll (integer)
- Branch(string)
- percent(float)
An array of structures is created that has 2 elements to store information of 2 students. Through the use of for loop, the program stores the information of 2 students entered by the user in an array of structure.
C
// C program to store data of // students and print them #include <stdio.h> #include <string.h> struct students
{ char first_Name[50];
char last_Name[50];
int roll_no;
char branch[50];
float percent;
} st[2]; // Driver code int main()
{ printf ( "Enter data of students\n" );
for ( int i = 0; i < 2; i++)
{
printf ( "Enter first name: " );
scanf ( "%s" , st[i].first_Name);
printf ( "Enter last name: " );
scanf ( "%s" , st[i].last_Name);
printf ( "Enter Branch: " );
scanf ( "%s" , st[i].branch);
printf ( "Enter Roll-No." );
scanf ( "%d" , &st[i].roll_no);
printf ( "Enter Percentage: " );
scanf ( "%f" , &st[i].percent);
}
printf ( "\n" );
printf ( "Displaying the Information: \n" );
for ( int i = 0; i < 2; i++)
{
printf ( "\nFirst name: " );
puts (st[i].first_Name);
printf ( "Last name: " );
puts (st[i].last_Name);
printf ( "Roll_No: %d" ,
st[i].roll_no);
printf ( "Branch: " );
puts (st[i].branch);
printf ( "Marks: %.1f" ,
st[i].percent);
}
return 0;
} |
Output:
Article Tags :
Recommended Articles