C Workshop · Student Struct
Lesson C

Student Struct

A student has more than one number: an id and marks. Keep them in one struct.

What it is

A Student record keeps more than one fact about the same person: an id and a mark. Brace initialization {1, 90} fills those fields in declaration order.

Approach

  1. Define struct Student { int id; int marks; };
  2. Initialize s with {1, 90}.
  3. Print id and marks.

Where this is used in the real world

Diagram

1

id

1

2

marks

90

3

one object

s

4

print

1 90

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2struct Student {
3    int id;
4    int marks;
5};
6int main(void) {
7    struct Student s = {1, 90};
8    printf("%d %d\n", s.id, s.marks);
9    return 0;
10}
What this line does
Press Run algorithm. The highlight will move through the C code.
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. Output:

id and marks.

2. {1, 90} fills fields in:

Initializer list.

3. A Student is one object so that:

Grouping.

4. Array of Student is:

Many records.

5. s.marks vs s->marks:

-> for pointers.