bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / assignment-primary / assignment-p-11.c

assignment-p-11.c (1800B)


      1 /* Write a C program that defines a structure Student containing the attributes rollNumber,
      2 name, and marks. Include a user-defined function named displayStudent with the
      3 signature void displayStudent(struct Student s);. The function should display the details
      4 of a student. */
      5 
      6 #include <stdio.h>
      7 #include <string.h>
      8 #include <stdlib.h>
      9 
     10 struct Student
     11 {
     12     int rollNumber;
     13     char name[50];
     14     float marks;
     15 };
     16 
     17 void inputStudent(struct Student *);
     18 void displayStudent(struct Student);
     19 
     20 int main()
     21 {
     22     struct Student *std = NULL;
     23     int i, n;
     24 
     25     printf("How many student details you want to add : ");
     26     if (scanf("%d", &n) != 1 || n < 1)
     27     {
     28         printf("\nInvalid Input.");
     29         return 1;
     30     }
     31 
     32     std = (struct Student *)malloc(n * sizeof(struct Student));
     33     if (std == NULL)
     34     {
     35         printf("\nUnable to allocate memory.");
     36         return 1;
     37     }
     38 
     39     for (i = 0; i < n; i++)
     40     {
     41         printf("\n- Enter details of Student %d -", i + 1);
     42         inputStudent(&std[i]);
     43     }
     44 
     45     printf("\n=== Student Details ===\n");
     46     for (i = 0; i < n; i++)
     47     {
     48         displayStudent(std[i]);
     49     }
     50 
     51     free(std);
     52     return 0;
     53 }
     54 
     55 void inputStudent(struct Student *std)
     56 {
     57     int len;
     58 
     59     printf("\nEnter the Roll Number: ");
     60     scanf("%d", &std->rollNumber);
     61     getchar();
     62     
     63     printf("Enter the Name (Max: 50 character): ");
     64     fgets(std->name, sizeof(std->name), stdin);
     65     len = strlen(std->name);
     66     if (len > 0 && std->name[len - 1] == '\n')
     67     {
     68         std->name[len - 1] = '\0';
     69     }
     70 
     71     printf("Enter the Marks: ");
     72     scanf("%f", &std->marks);
     73 }
     74 
     75 void displayStudent(struct Student std)
     76 {
     77     printf("\n%-12s : %d", "Roll Number", std.rollNumber);
     78     printf("\n%-12s : %s", "Name", std.name);
     79     printf("\n%-12s : %g\n", "Marks", std.marks);
     80 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror