assignment-p-11.c (1992B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 12 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a C program that defines a structure Student containing the attributes rollNumber, 9 name, and marks. Include a user-defined function named displayStudent with the 10 signature void displayStudent(struct Student s);. The function should display the details 11 of a student. */ 12 13 #include <stdio.h> 14 #include <string.h> 15 #include <stdlib.h> 16 17 struct Student 18 { 19 int rollNumber; 20 char name[50]; 21 float marks; 22 }; 23 24 void inputStudent(struct Student *); 25 void displayStudent(struct Student); 26 27 int main() 28 { 29 struct Student *std = NULL; 30 int i, n; 31 32 printf("How many student details you want to add : "); 33 if (scanf("%d", &n) != 1 || n < 1) 34 { 35 printf("\nInvalid Input."); 36 return 1; 37 } 38 39 std = (struct Student *)malloc(n * sizeof(struct Student)); 40 if (std == NULL) 41 { 42 printf("\nUnable to allocate memory."); 43 return 1; 44 } 45 46 for (i = 0; i < n; i++) 47 { 48 printf("\n- Enter details of Student %d -", i + 1); 49 inputStudent(&std[i]); 50 } 51 52 printf("\n=== Student Details ===\n"); 53 for (i = 0; i < n; i++) 54 { 55 displayStudent(std[i]); 56 } 57 58 free(std); 59 return 0; 60 } 61 62 void inputStudent(struct Student *std) 63 { 64 int len; 65 66 printf("\nEnter the Roll Number: "); 67 scanf("%d", &std->rollNumber); 68 getchar(); 69 70 printf("Enter the Name (Max: 50 character): "); 71 fgets(std->name, sizeof(std->name), stdin); 72 len = strlen(std->name); 73 if (len > 0 && std->name[len - 1] == '\n') 74 { 75 std->name[len - 1] = '\0'; 76 } 77 78 printf("Enter the Marks: "); 79 scanf("%f", &std->marks); 80 } 81 82 void displayStudent(struct Student std) 83 { 84 printf("\n%-12s : %d", "Roll Number", std.rollNumber); 85 printf("\n%-12s : %s", "Name", std.name); 86 printf("\n%-12s : %g\n", "Marks", std.marks); 87 }