Nearby lessons

98 of 124

C - Structures

Structures is one of the foundational topics in C programming. This lesson explains Why Structures?, Defining a Structure and Program 1: Declare Variables and Access Members with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Why Structures?

Arrays hold many values of the same type. But a student has a name (string), a roll number (int), and marks (float) — different types. A structure groups these different types under one name.

Think of a structure as a form: the Student form has one box for name, one for roll number, one for marks — all carried together as one unit.

In simple words: arrays hold many of the same thing; structures hold different things about the same person. One student = one structure with mixed fields.

Defining a Structure

This only creates a template (a design). It does not reserve memory. Memory is reserved when we declare variables of this type.

Example02
CCode Cell
1struct student {
2 int rollNo; // member 1
3 char name[30]; // member 2
4 float marks; // member 3
5}; // note the semicolon

Program 1: Declare Variables and Access Members

The dot operator (.) is how you reach a member. It is also called the member access operator.

In simple words: the dot is the "reach into the box" operator. s1.rollNo means open box s1 and pull out the rollNo slot.
Example03
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4struct student {
5 int rollNo;
6 char name[30];
7 float marks;
8};
9 
10void main()
11{
12 struct student s1; // variable of type struct student
13 
14 s1.rollNo = 101; // member access with the dot (.)
15 strcpy(s1.name, "Rahul");
16 s1.marks = 88.5;
17 
18 printf("%d %s %.2f\n", s1.rollNo, s1.name, s1.marks);
19}
Output
101 Rahul 88.50

Program 2: Initializing in One Line

Example04
CCode Cell
1#include <stdio.h>
2 
3struct student {
4 int rollNo;
5 char name[30];
6 float marks;
7};
8 
9void main()
10{
11 struct student s1 = {101, "Rahul", 88.5}; // values in order
12 struct student s2 = {102, "Priya", 95.0};
13 
14 printf("%d %s %.2f\n", s1.rollNo, s1.name, s1.marks);
15 printf("%d %s %.2f\n", s2.rollNo, s2.name, s2.marks);
16}
Output

101 Rahul 88.50
102 Priya 95.00
      

Program 3: Take a Student's Data from the User

Example05
CCode Cell
1#include <stdio.h>
2 
3struct student {
4 int rollNo;
5 char name[30];
6 float marks;
7};
8 
9void main()
10{
11 struct student s;
12 
13 printf("Enter roll number: ");
14 scanf("%d", &s.rollNo);
15 printf("Enter name: ");
16 scanf("%s", s.name); // note: no & for strings
17 printf("Enter marks: ");
18 scanf("%f", &s.marks);
19 
20 printf("%d %s %.2f\n", s.rollNo, s.name, s.marks);
21}
Output

Enter roll number: 101
Enter name: Rahul
Enter marks: 88.5
101 Rahul 88.50
      

Structure vs Array — One-Line Memory

PointArrayStructure
MembersSame typeCan be different types
AccessWith index a[0]With member s.name
Use forA list of similar valuesA record with mixed fields
📝 Key Takeaways
  • Structure groups different types under one name — like a form.
  • Define the template, declare variables, access members with the dot.
  • Separate programs: access, one-line init, user input, arrays of structures.
  • Structures can nest inside structures.
  • Union shares one memory for all members; structure gives each member its own.
  • sizeof(union) = largest member; sizeof(struct) = sum of members.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4