If you don't know the number of students at the time of coding this program you can always use linked lists and malloc to allocate memory dynamically although it's not safe especially if you have very limited memory resources for example in embedded systems case.
your code will be
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
struct LinkedList{
char name[10];
int id;
float marks;
char grade;
struct LinkedList *next;
};
typedef struct LinkedList *node; //Define node as a pointer of data type struct LinkedList
node createNode(){
node temp; // declare a node
temp = (node)malloc(sizeof(struct LinkedList)); // allocate memory using malloc()
temp->next = NULL;// make next point to NULL
return temp;//return the new node
}
node addNode(node head,char* name, int id, float marks, char grade){
node temp,p;// declare two nodes temp and p
temp = createNode();//createNode will return a new node with data = value and next pointing to NULL.
strncpy(temp->name, name, 10); // add element's data part of node
temp->id = id; // add element's data part of node
temp->marks = marks; // add element's data part of node
temp->grade = grade; // add element's data part of node
if(head == NULL){
head = temp; //when linked list is empty
}
else{
p = head;//assign head to p
while(p->next != NULL){
p = p->next;//traverse the list until p is the last node.The last node always points to NULL.
}
p->next = temp;//Point the previous last node to the new node created.
}
return head;
}
char i, x;
node head; // declare The head
char name[10];
int id;
float marks;
char grade;
int main() {
printf("If you want to quit press q ,to continue press anything else");
i = getch();
while(i !='q'){
printf("\n Name : ");
fgets(name, 10, stdin);
printf("\n Id : ");
scanf("%d", &id);
printf("\n Marks : ");
scanf("%f", &marks);
printf("\n Grade : ");
scanf(" %c", &grade);
printf("\n name: %s id: %d marks: %.2f grade: %c\n", name, id, marks, grade);
addNode(head, name, id, marks, grade);
x = 'y';
do{
printf("\n If you want to quit press q ,to continue press anything else");
i = getch();
if(i=='q'){
printf("\n Are you sure you want to quit?");
printf("\n press y: for yes anything else: for NO");
x = getch();
}
}while(x !='y');
}
return 0;
}