0% found this document useful (0 votes)
2 views

C Programming II - Structure and Union_f2b41c09-2080-4a6a-8836-0d4ee6626fb0

The document provides an overview of structures and unions in C programming, highlighting their definitions, usage, and differences. Structures are user-defined data types that group related data items of different types, while unions allow for the storage of different data types in the same memory location, conserving memory. The document includes examples, syntax, and class assignments related to both structures and unions.

Uploaded by

lekhakbikrant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Programming II - Structure and Union_f2b41c09-2080-4a6a-8836-0d4ee6626fb0

The document provides an overview of structures and unions in C programming, highlighting their definitions, usage, and differences. Structures are user-defined data types that group related data items of different types, while unions allow for the storage of different data types in the same memory location, conserving memory. The document includes examples, syntax, and class assignments related to both structures and unions.

Uploaded by

lekhakbikrant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

C Programming II

Computer Science, XII


St. Xavier’s College
STRUCTURES & UNIONS

Computer Science
Why use Structure?
▪A convenient method of handling a group of
related data items of different data types.
Structure
▪User defined data type.
▪A method of grouping data of different types.
▪ Data must be logically connected to one another.
▪ A structure variable must be initialized to access the data
members in the main function.
Structure Definition
▪Must be defined in terms of its
individual members.
○ struct: required keyword
○ structureName: identifies
structure
○ member: individual members
▪ Defined top of main() function.
Structure Declaration
▪Declaration of individual structure variables.
▪ Syntax: struct tag var1, var2, …var n;
Structure Initialization
▪Assignment of initial values in structure variables.
▪ Must be assigned in order.
▪ Syntax: struct tag variable = {val1, val2,...val n};
Structure Size
▪ Size of a structure is sum of all data members.
Accessing a Member of Structure
▪ Syntax: variable.member
○ variable: name of the structure variable
○ member: name of member inside structure
Example
Example to take user input in Structure
Classwork
▪ Take data of two books using Structure and print them.
▪ Members: id, name, price
Nested Structure
▪Structure within a
structure.
▪ Mainly used for complex
data types.
Accessing Members in Nested Structure
Array of Structure
▪Collection of multiple
structures variables.
▪ Each variable contains
information about different
entities.
▪ Also known as Collection of
Structures.
Array of Structure Example
Array of
Structure
Example
Array of Structure Example
Array of Structure Example Output
Class/Home Assignment
▪ Write a C program that takes first_name, last_name, age
of 3 employees and prints the record in ascending order
on the basis of age using structure.
Passing Structure to Function
Passing
Structure to
Function
Passing Structure to Function
Union
▪ User defined data type.
▪ Collection of variables of different data types in the same
memory location.
▪ Finds which of its member needs high storage space over
other members and allocates that much space.
▪ At a given point of time only one member can contain a
value.
Usage of Union
▪ Used to conserve memory.
▪ Useful for applications involving multiple members
where values not needed to be assigned to all the
members at any one time.
▪ Example: embedded systems like tv remote, traffic
lights.
Syntax of Union
▪ Declaration and
initialization same as
Structure.
Example of Union
Difference between Structure and Union
Structure Union
Allocates storage space for all Allocates common storage for
of its members. all members with maximum
storage capacity of particular
member.
Occupies higher memory Occupies lower memory
space. space.
All members of structure can One member can be
be accessed at a time. accessed at a time.
Keyword: struct Keyword: union
Program to demonstrate storage size
difference between structure and
union
Program to demonstrate storage size
difference between structure and union
THANK YOU!

Computer Science THE END

You might also like