This C program allows users to input the number of students, store student records as a structure using pointers and prints student information. In this C Program below we will store the student’s name, roll, age and CGPA and eventually print them on the console.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[30];
long int roll;
int age;
float cgpa;
};
int main()
{
int num_students;
struct Student *student;
printf("Please enter the number of students: ");
scanf("%d", &num_students);
student = (struct Student *) malloc(num_students * sizeof(struct Student));
for(int i=1; i<= num_students; i++)
{
printf("Please enter student %d's info: \n", i);
printf("Name: ");
scanf("%s", (student+i)->name);
printf("Roll: ");
scanf("%ld", &(student+i)->roll);
printf("Age: ");
scanf("%d", &(student+i)->age);
printf("CGPA: ");
scanf("%f", &(student+i)->cgpa);
printf("\n");
}
for(int i=1; i<= num_students; i++)
{
printf("Student %d info: \n", i);
printf("Name: %s\n", (student+i)->name);
printf("Roll: %ld\n", (student+i)->roll);
printf("Age: %d\n", (student+i)->age);
printf("CGPA: %f\n", (student+i)->cgpa);
printf("\n");
}
return 0;
}
Output:
Leave a Reply