C Programming • Structure and Union
C Programming / Introduction to structures

Introduction to structures

Notes 4 Structure and Union

A structure is a user-defined data type in C that allows related variables of different data types to be grouped together under one name.

Notes

Introduction to Structures

A structure is a user-defined data type in C that allows related variables of different data types to be grouped together under one name.

💡 Example

A student record may contain a roll number, name and percentage. These different values can be grouped into one structure called Student.

Why Use a Structure?

Structures are used when different pieces of related information need to be treated as one logical record. They make related data easier to organize and manage.

💡 Example

A student has a roll number (int), percentage (float), and grade (char). A structure can store all three as one student record.

Syntax of Structure

struct structure_name { data_type member1; data_type member2; ... };
💡 Example
struct Student { int rollNo; float percentage; char grade; };

Here, Student is the structure name and rollNo, percentage and grade are its members.

Structure Members

The variables declared inside a structure are called structure members. Different members can have different data types.

💡 Example
int rollNo; float percentage; char grade;

Here, rollNo, percentage and grade are structure members.

Declaring a Structure Variable

After defining a structure, a variable of that structure type can be declared to store a record.

💡 Example
struct Student s1;

Here, s1 is a structure variable of type struct Student.

Accessing Structure Members

The dot operator (.) is used to access the members of a structure variable.

💡 Example
s1.rollNo = 101; s1.percentage = 82.5;

Here, the dot operator is used to access the rollNo and percentage members of the structure variable s1.

Structure Initialization

A structure variable can be initialized when it is declared by providing values for its members in the same order in which they are declared.

💡 Example
struct Student s1 = {101, 82.5, 'A'};

The values are assigned to rollNo, percentage and grade, respectively.

Simple Structure Program

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

Expected Output

Roll Number = 101

Percentage = 82.5

Practical Example

Problem Statement

Write a C program to define a structure named Student, accept a student's roll number and percentage, and display the stored information.

Learning Outcomes

  • Define a structure with different data types.
  • Declare a structure variable.
  • Access structure members using the dot operator.

Hint

Create a Student structure with rollNo and percentage members. Use the dot operator to access them.

Theory

A structure groups related data under one name. After declaring a structure variable, its individual members can be accessed using the dot operator.

Program

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

Expected Output

Enter roll number: 101

Enter percentage: 82.5

Roll Number = 101

Percentage = 82.5

Note

A structure is useful for representing a complete record containing related values of different data types. The dot operator is used to access individual members of a structure variable.

Quick Revision

Concept Remember
Structure User-defined data type that groups related variables.
Member Variable declared inside a structure.
Structure Variable Variable used to store a structure record.
Dot Operator (.) Used to access structure members.

Important Exam Questions

Short Answer Questions

  1. What is a structure in C?
  2. Why are structures used?
  3. What are structure members?
  4. How is a structure variable declared?
  5. Which operator is used to access structure members?
  6. Write the syntax of a structure.

Long Answer Questions

  1. Explain structures in C with syntax and suitable example.
  2. Explain how structure variables and structure members are declared and accessed.
  3. Write a C program to store and display student information using a structure.
🎥 Recommended Learning

Watch a beginner-friendly explanation of structures in C.

▶ Watch: Structure in C — Hindi

📝 Handwritten Notes

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

🧠 Mind Map

Use the mind map for quick revision of structure definition, members, variables and member access.