C Programming • Structure and Union
C Programming / Introduction

Introduction

Notes 4 Structure and Union

C provides special data types that allow a programmer to group related data and represent complex information in a program. The two important concepts covered in this module are structures and unions.

Notes

Introduction to Structure and Union

C provides special data types that allow a programmer to group related data and represent complex information in a program. The two important concepts covered in this module are structures and unions.

💡 Example

A student record may contain a name, roll number and marks. These related values can be grouped together using a structure.

Why Do We Need Structures?

A structure allows different types of related data to be stored together under one variable name. This makes a program easier to organize and represent real-world records.

💡 Example

A student can have an integer roll number, a character grade, and a floating-point percentage. A structure can group all these values into one student record.

Real-World Example

Consider a student database. Information such as Roll Number, Name, Course, and Percentage belongs to the same student. Treating them as one record makes the data easier to manage.

🌍 Example

One student record can be represented as: Roll No. = 101, Name = Ravi, Percentage = 82.5.

Structure

A structure is a user-defined data type that groups variables of different data types under one name.

💡 Example
struct Student { int rollNo; char grade; float percentage; };

Here, Student contains three related members: rollNo, grade and percentage.

Union

A union is also a user-defined data type that groups different types of members under one name, but its members share the same memory location.

💡 Example
union Data { int number; float value; char grade; };

In a union, the members share memory, so only one member is normally used to hold a meaningful value at a time.

Structure vs Union

Structure Union
Members have separate memory locations. Members share the same memory location.
Multiple members can hold values at the same time. Generally, one member is used at a time.
Useful for representing complete records. Useful when memory needs to be shared among alternatives.

Simple Structure Program

#include <stdio.h> struct Student { int rollNo; float percentage; }; int main() { struct Student s; s.rollNo = 101; s.percentage = 82.5; // display student record printf("Roll Number = %d ", s.rollNo); printf("Percentage = %.1f ", s.percentage); return 0; }

Expected Output

Roll Number = 101

Percentage = 82.5

Practical Example

Problem Statement

Write a C program to define a structure for a student and display the student's roll number and percentage.

Learning Outcomes

  • Understand the basic idea of a structure.
  • Create a structure with members of different data types.
  • Store and display values using a structure variable.

Hint

Define a structure named Student with an integer roll number and a floating-point percentage.

Theory

A structure groups related variables of different data types under one name. A structure variable is then used to store values for those members.

Program

#include <stdio.h> struct Student { int rollNo; float percentage; }; int main() { struct Student s; printf("Enter roll number: "); scanf("%d", &s.rollNo); printf("Enter percentage: "); scanf("%f", &s.percentage); // display the stored student information printf("Roll Number = %d ", s.rollNo); printf("Percentage = %.1f ", s.percentage); return 0; }

Expected Output

Enter roll number: 101

Enter percentage: 82.5

Roll Number = 101

Percentage = 82.5

Note

A structure is useful when several related values, possibly of different data types, need to be treated as one logical record.

Quick Revision

Concept Remember
Structure Groups related variables of different data types.
Union Groups members that share the same memory location.
Structure Member A variable declared inside a structure.
Structure Variable Variable used to store a structure record.

Important Exam Questions

Short Answer Questions

  1. What is a structure in C?
  2. Why are structures used?
  3. What is a union in C?
  4. What is the main difference between a structure and a union?
  5. What is a structure member?

Long Answer Questions

  1. Explain the concept of structures in C with a suitable example.
  2. Explain structures and unions and differentiate between them.
  3. Write a C program to store and display student information using a structure.
🎥 Recommended Learning

Watch a beginner-friendly introduction to structures and unions in C programming.

▶ Watch: Structures & Unions in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for structures and unions will be provided here.

🧠 Mind Map

Use the mind map for quick revision of structure, union, members and memory sharing.