assignment-p-11.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 12 Dec 2025
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Write a C program that defines a structure Student containing the attributes rollNumber, name, and marks. Include a user-defined function named displayStudent with the signature void displayStudent(struct Student s);. The function should display the details of a student.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Student
{
int rollNumber;
char name[50];
float marks;
};
void inputStudent(struct Student *);
void displayStudent(struct Student);
int main()
{
struct Student *std = NULL;
int i, n;
printf("How many student details you want to add : ");
if (scanf("%d", &n) != 1 || n < 1)
{
printf("\nInvalid Input.");
return 1;
}
std = (struct Student *)malloc(n * sizeof(struct Student));
if (std == NULL)
{
printf("\nUnable to allocate memory.");
return 1;
}
for (i = 0; i < n; i++)
{
printf("\n- Enter details of Student %d -", i + 1);
inputStudent(&std[i]);
}
printf("\n=== Student Details ===\n");
for (i = 0; i < n; i++)
{
displayStudent(std[i]);
}
free(std);
return 0;
}
void inputStudent(struct Student *std)
{
int len;
printf("\nEnter the Roll Number: ");
scanf("%d", &std->rollNumber);
getchar();
printf("Enter the Name (Max: 50 character): ");
fgets(std->name, sizeof(std->name), stdin);
len = strlen(std->name);
if (len > 0 && std->name[len - 1] == '\n')
{
std->name[len - 1] = '\0';
}
printf("Enter the Marks: ");
scanf("%f", &std->marks);
}
void displayStudent(struct Student std)
{
printf("\n%-12s : %d", "Roll Number", std.rollNumber);
printf("\n%-12s : %s", "Name", std.name);
printf("\n%-12s : %g\n", "Marks", std.marks);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75