0% found this document useful (0 votes)
6 views20 pages

Unions and Storage Classes in C

Uploaded by

theerthashreegs
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)
6 views20 pages

Unions and Storage Classes in C

Uploaded by

theerthashreegs
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

Seminar on Union and

Storage Class in C
 Presented by:
• Chetan Shetty
• Avinash Shetty
• Chetan B
Introduction to Unions

 A union is a user-defined data type in C that


allows storing multiple variables of different
data types in the same memory location.
Unlike structures, only one member can store
a value at a time because they share the
same memory.

 EX- Think of a hotel room that can either have


a bed or a sofa but not both at the same time.
Similarly, a union allows different data types to
share the same memory space, but only one
can be active at a time.
Declaring a Union

 Declaring a union is similar to declaring a


structure, but instead of allocating memory for
all members, it allocates memory equal to
the largest member.
 EX-
Explanation:

 Union Data contains an integer, a float, and a


character array.
 The union allocates memory equal to the
size of the largest member.
 The memory location will be shared among i , f
and str.
 Real World Example:-
A USB drive that can store documents,
music, or
videos, but the total space remains the same.
Accessing a Member of a
Union
 A union member can be accessed using
the dot (.) operator, just like structures.
However, assigning a value to one member
will overwrite any previously stored data.
 EX-
Explanation:

 data.i =10; assigns a value to the integer


member.
 Printing data.i displays 10 as expected.
 If another member is assigned a value, data.i
would be overwritten.
 Real World Example:-
A bank account where the available balance or
the loan amount is shown, but not both at the
same time.
Initializing a Union

 A union can be initialized at the time of


declaration by assigning a value to its first
member. If we try to initialize multiple
members, only the last one will be stored.
 EX-
Explanation:

 The union is initialized with 5, which is assigned


to i.
 The memory is allocated only for the largest
member.
 Real-World Example:
A smart remote that can control TV, AC, or
Music System, but only one device at a time.
Arrays of Union Variables
 An array of unions allows storing multiple
union variables, but each element in the
array will behave like an independent
union (i.e., only one member can be active in
each union variable at a time).
 EX-
Explanation:

 arr[0] stores an integer, and arr[1] stores a


float.
 Since they are separate elements of the array,
they do not overwrite each other..
 Real-World Example:
A set of lockers where each locker can store
either books, gadgets, or clothes, but each locker
holds only one type of item at a time.
Unions Inside Structures
 A union inside a structure means a structure
contains a union as one of its members. This is
useful when an entity needs to store different
types of data but uses only one at a time.
 EX-
Explanation:

 The structure container has an integer type


and a union value.
 The union can store either an integer or a
float, but only one at a time.
 The program assigns 100 to [Link].i, and
prints it.
 Real-World Example:
A vending machine where pressing a button
selects either a snack or a drink, but not both.
Structures Inside Unions
 A structure inside a union means a union
contains a structure as one of its members. This
is useful when we need a structured
representation of data but only one set of
information at a time.
 EX-
Explanation:

 Union Info contains an integer id and a


structure person with name and age.
 Since it is a union, only one of them can be
used at a time.
 The program assigns 25 to [Link] and
prints it.
 Real-World Example:
A train ticket where either passenger details
or a QR code is stored, but not both at the same
time.
Enumerated Data Type

 An Enumerated Data Type (enum) in C is a


user-defined type that assigns names to
integral constants. It makes the code more
readable and manageable by replacing
numbers with meaningful names.
 EX-
Explanation:

 The enum Days assigns values starting from 0


(SUNDAY) to 6 (SATURDAY).
 When we set today = MONDAY, it holds the
value 1.
 The program prints 1 because MONDAY is the
second element (index 1) in the enum.
 Real-World Example:
Traffic lights where only one state (Red, Yellow,
Green) is active at a time. Similarly, in an enum,
only one value is selected at a time.
Storage Class and
Visibility
 A storage class in C defines the scope
(visibility) and lifetime of a variable. It tells
whether a variable is local, global, static, or
extern.
 EX-
Explanation:

 The variable count is declared as static , so it


retains its value between function calls.
 The first call prints 1, and the second call prints
2.
 Real-World Example:
A hotel loyalty card where points are retained
between visits.
Difference Between
Union and Structure
Feature Structure Union
Memory Allocates memory Allocates memory
for all members for the largest
member only
Access Only one member
All members can be
can be accessed at a
accessed at once
time
Use Case Used when storing Used when storing
multiple different values but
independent values only one is needed
at a time
Conclusion

 Unions optimize memory when different values


are needed at different times.
 Storage classes manage variable scope and
lifetime efficiently.
 Applying these concepts improves C
programming efficiency.

You might also like