SlideShare a Scribd company logo
UNIONS IN C
UNIONS
• A union is a special data type available in C that allows to store
different data types in the same memory location.
• You can define a union with many members, but only one member can
contain a value at any given time.
• Unions provide an efficient way of using the same memory location
for multiple-purpose.
DEFINING A UNION
• To define a union, you must use the union statement in the same way as you
did while defining a structure.
• The union statement defines a new data type with more than one member for
your program. The format of the union statement is as follows −
union [union tag] {
member definition;
member definition;
...
member definition;
} [one or more union variables];
CREATING A UNION VARIABLE
• When a union is defined, it creates a user-defined type. However, no
memory is allocated.
• To allocate memory for a given union type and work with it, we need
to create variables.
• The memory occupied by a union will be large enough to hold the
largest member of the union
ACCESSING UNION MEMBERS
• To access any member of a union, we use the member access operator
(.).
• The member access operator is coded as a period between the union
variable name and the union member that we wish to access.
SIZE OF UNION
• The size of the union is based on the size of
the largest member of the union.
• As we know, the size of int is 4 bytes, size
of char is 1 byte, size of float is 4 bytes, and
the size of double is 8 bytes.
• Since the double variable occupies the
largest memory among all the four
variables, so total 8 bytes will be allocated
in the memory.
• Therefore, the output of the program would
be 8 bytes.
ACCESSING MEMBERS USING POINTERS
• We can access the members of the
union through pointers by using
the (->) arrow operator.
• In the above code, we have
created a pointer variable, i.e.,
*ptr, that stores the address of var
variable.
• Now, ptr can access the variable
'a' by using the (->) operator.
• Hence the output of the above
code would be 90.
STRUCTURE V/S UNION
UNIONS IN C.pptx
UNIONS IN C.pptx
11
UNIONS IN C.pptx
UNIONS IN C.pptx

More Related Content

PPTX
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
PPTX
Union in C programming
PPTX
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
PPTX
UNION 1.pptx
PDF
C- language Lecture 7
PPTX
Unions.pptx
ODP
C union
PPTX
Lesson 26 c programming ( union, storage classes)
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Union in C programming
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
UNION 1.pptx
C- language Lecture 7
Unions.pptx
C union
Lesson 26 c programming ( union, storage classes)

Similar to UNIONS IN C.pptx (20)

PPTX
Unit 9. Structure and Unions
PPTX
Union in c language
PPTX
Union in c language
DOCX
Union
DOCX
Union from C and Data Strutures
PPT
Union In language C
PPTX
data types in C programming
PDF
cassignmentii-170424105623.pdf
PPSX
PPT
PPTX
Programming in C
PPTX
CS4443 - Modern Programming Language - I Lecture (2)
PDF
C programming session7
PDF
C programming session7
DOC
Structures unions
PPTX
17 structure-and-union
PPTX
Variables in C and C++ Language
PPTX
Data Types in C language
PPTX
discuss about the union & structure in python
PPTX
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Unit 9. Structure and Unions
Union in c language
Union in c language
Union
Union from C and Data Strutures
Union In language C
data types in C programming
cassignmentii-170424105623.pdf
Programming in C
CS4443 - Modern Programming Language - I Lecture (2)
C programming session7
C programming session7
Structures unions
17 structure-and-union
Variables in C and C++ Language
Data Types in C language
discuss about the union & structure in python
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Ad

More from SKUP1 (20)

PPTX
serial_busses_i2c.pptx
PPTX
DESIGN PATTERN.pptx
PPTX
INTER PROCESS COMMUNICATION (IPC).pptx
PPTX
DATA STRUCTURES AND LINKED LISTS IN C.pptx
PPTX
C-Programming File-handling-C.pptx
PPTX
Processes, Threads.pptx
PPTX
C-Programming Control statements.pptx
PPTX
Finite State Machine.ppt.pptx
PPTX
FUNCTIONS IN C.pptx
PPTX
cprogramming strings.pptx
PPTX
OPERATORS IN C.pptx
PPTX
cprogramming Structures.pptx
PPTX
C-Programming Function pointers.pptx
PPTX
POINTERS.pptx
PPTX
STACKS AND QUEUES.pptx
PPTX
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
PPTX
C MEMORY MODEL​.pptx
PPTX
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
COMPILATION PROCESS IN C.pptx
serial_busses_i2c.pptx
DESIGN PATTERN.pptx
INTER PROCESS COMMUNICATION (IPC).pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
C-Programming File-handling-C.pptx
Processes, Threads.pptx
C-Programming Control statements.pptx
Finite State Machine.ppt.pptx
FUNCTIONS IN C.pptx
cprogramming strings.pptx
OPERATORS IN C.pptx
cprogramming Structures.pptx
C-Programming Function pointers.pptx
POINTERS.pptx
STACKS AND QUEUES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C MEMORY MODEL​.pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DYNAMIC MEMORY ALLOCATION.pptx
COMPILATION PROCESS IN C.pptx
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
web development for engineering and engineering
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Construction Project Organization Group 2.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Well-logging-methods_new................
PPTX
Geodesy 1.pptx...............................................
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Welding lecture in detail for understanding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPT on Performance Review to get promotions
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
web development for engineering and engineering
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Construction Project Organization Group 2.pptx
Mechanical Engineering MATERIALS Selection
Model Code of Practice - Construction Work - 21102022 .pdf
Well-logging-methods_new................
Geodesy 1.pptx...............................................
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Automation-in-Manufacturing-Chapter-Introduction.pdf
OOP with Java - Java Introduction (Basics)
bas. eng. economics group 4 presentation 1.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Welding lecture in detail for understanding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS

UNIONS IN C.pptx

  • 2. UNIONS • A union is a special data type available in C that allows to store different data types in the same memory location. • You can define a union with many members, but only one member can contain a value at any given time. • Unions provide an efficient way of using the same memory location for multiple-purpose.
  • 3. DEFINING A UNION • To define a union, you must use the union statement in the same way as you did while defining a structure. • The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows − union [union tag] { member definition; member definition; ... member definition; } [one or more union variables];
  • 4. CREATING A UNION VARIABLE • When a union is defined, it creates a user-defined type. However, no memory is allocated. • To allocate memory for a given union type and work with it, we need to create variables. • The memory occupied by a union will be large enough to hold the largest member of the union
  • 5. ACCESSING UNION MEMBERS • To access any member of a union, we use the member access operator (.). • The member access operator is coded as a period between the union variable name and the union member that we wish to access.
  • 6. SIZE OF UNION • The size of the union is based on the size of the largest member of the union. • As we know, the size of int is 4 bytes, size of char is 1 byte, size of float is 4 bytes, and the size of double is 8 bytes. • Since the double variable occupies the largest memory among all the four variables, so total 8 bytes will be allocated in the memory. • Therefore, the output of the program would be 8 bytes.
  • 7. ACCESSING MEMBERS USING POINTERS • We can access the members of the union through pointers by using the (->) arrow operator. • In the above code, we have created a pointer variable, i.e., *ptr, that stores the address of var variable. • Now, ptr can access the variable 'a' by using the (->) operator. • Hence the output of the above code would be 90.
  • 11. 11