SlideShare a Scribd company logo
C union
Defination & introduction
• Unions are con conceptually similar to structures
• The syntax of union is also similar to that of structure
• The only difference is in term of storage
• In structure each member has its own storage location where
the all members of union uses a single shared memory
location which is equal to the size of its largest data
member .
• Data can of int,char,float,double etc .data type
• It is a user defined data type
Note-> union does not support multiple value it can store
only one value at a time
•
structure union
Struct emp; union emp
{ {
Char x; //size 1 byte char x;
Float y;//size 4 byte float y;
}e;}e; memory sharing
x y x&y
5 byte 4 byte
syntax
Keyword→ union structure _ name
{
data_ type variable 1;
Body data_type variable 2;
…………………………….
data _type variable n;
};
example
Union student
{
char name[200];
Int rollno;
Float marks;
};
1.Here student is a the name of union
Declaration of union variable
method 1
Union student
{
Char name[200];
Int roll no;
Float marks;
};
Int main()
{
Union student student1;
Return 0;
}
Declaration of union variable
method 2
Union student
{
Char name[200];
Int rollno;
Float marks;
}student1;
Int main()
{
Return 0;
}
Accessing the data members of
union
• The data member of union can be
accessed as
Union_variable.data_mamber
Example
The rollno of student then we can write as
Student1.rollno
Program of union• #include<stdio.h>
• #include<string.h>
• union student
• {
• char name[200];
• int rollno;
• float marks;
•
• }student1;
• int main()
• {
•
• strcpy(student1.name,"ali");
• student1.rollno=201;
• student1.marks=78.5;
• printf("student name=%sn",student1.name);
• printf("student roll no=%dn",student1.rollno);
• printf("student marks=%fn",student1.marks);
• return 0;
• }
Program of union• #include<stdio.h>
• #include<string.h>
• union student
• {
• char name[200];
• int rollno;
• float marks;
•
• }student1;
• int main()
• {
•
• strcpy(student1.name,"ali");
• student1.rollno=201;
• student1.marks=78.5;
• printf("student name=%sn",student1.name);
• printf("student roll no=%dn",student1.rollno);
• printf("student marks=%fn",student1.marks);
• return 0;
• }

More Related Content

PPTX
Union in C programming
PPTX
Union in c language
PPTX
Sessions in php
PPTX
Object Oriented Programming Using C++
PPTX
6.origins genesis of .net technology
PPTX
Introduction to Operating Systems
PPTX
Arrays in c
PPTX
Loops in C Programming Language
Union in C programming
Union in c language
Sessions in php
Object Oriented Programming Using C++
6.origins genesis of .net technology
Introduction to Operating Systems
Arrays in c
Loops in C Programming Language

What's hot (20)

PDF
Typical Configuration of Computer System
PPTX
Text Editor in System software
PPTX
Unit 9. Structure and Unions
PPT
Looping statements in Java
PPTX
String Handling in c++
PPT
File handling in c
PPT
Enumerated data types in C
PPTX
Segmentation in Operating Systems.
PPT
Java Streams
PPTX
PPTX
data types in C programming
PPT
Java-java virtual machine
PPTX
C keywords and identifiers
PPTX
Memory interleaving
PPSX
computer language - Html frames
PPTX
File system structure
PPT
Structures
PPT
Cookies and sessions
PPTX
Macros...presentation
PPTX
Structure in C
Typical Configuration of Computer System
Text Editor in System software
Unit 9. Structure and Unions
Looping statements in Java
String Handling in c++
File handling in c
Enumerated data types in C
Segmentation in Operating Systems.
Java Streams
data types in C programming
Java-java virtual machine
C keywords and identifiers
Memory interleaving
computer language - Html frames
File system structure
Structures
Cookies and sessions
Macros...presentation
Structure in C
Ad

Similar to C union (20)

PPTX
UNION 1.pptx
DOC
Structures unions
PPTX
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
DOCX
Union from C and Data Strutures
PPTX
17 structure-and-union
PPTX
Udiyann Dutta DSC ca1 ppt.pptxfxifxitxixit
PPTX
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
PPTX
UNIONS IN C.pptx
PPTX
UNIONS IN C.pptx
PPTX
Lesson 26 c programming ( union, storage classes)
PPTX
Unions.pptx
DOCX
Union
PPTX
Union in c language
PPT
structure and union from C programming Language
PPT
structure and union from c programming language.ppt
PDF
C Programming Lab - Session 7 - Arrays, String, Structure and Union.pdf
PDF
Union in C Programming Language-Basic Concepts
PPTX
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
PPSX
PDF
C- language Lecture 7
UNION 1.pptx
Structures unions
Unit6STRUCTUREANDUNIONpptx__2024_11_18_12_20_46.pptx
Union from C and Data Strutures
17 structure-and-union
Udiyann Dutta DSC ca1 ppt.pptxfxifxitxixit
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
UNIONS IN C.pptx
UNIONS IN C.pptx
Lesson 26 c programming ( union, storage classes)
Unions.pptx
Union
Union in c language
structure and union from C programming Language
structure and union from c programming language.ppt
C Programming Lab - Session 7 - Arrays, String, Structure and Union.pdf
Union in C Programming Language-Basic Concepts
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
C- language Lecture 7
Ad

Recently uploaded (20)

PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PPTX
Tech Workshop Escape Room Tech Workshop
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
MCP Security Tutorial - Beginner to Advanced
PPTX
Introduction to Windows Operating System
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Types of Token_ From Utility to Security.pdf
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Trending Python Topics for Data Visualization in 2025
PPTX
GSA Content Generator Crack (2025 Latest)
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
Time Tracking Features That Teams and Organizations Actually Need
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Tech Workshop Escape Room Tech Workshop
Computer Software and OS of computer science of grade 11.pptx
Monitoring Stack: Grafana, Loki & Promtail
MCP Security Tutorial - Beginner to Advanced
Introduction to Windows Operating System
Why Generative AI is the Future of Content, Code & Creativity?
Types of Token_ From Utility to Security.pdf
Complete Guide to Website Development in Malaysia for SMEs
Digital Systems & Binary Numbers (comprehensive )
Trending Python Topics for Data Visualization in 2025
GSA Content Generator Crack (2025 Latest)
Weekly report ppt - harsh dattuprasad patel.pptx
Oracle Fusion HCM Cloud Demo for Beginners
Time Tracking Features That Teams and Organizations Actually Need
Advanced SystemCare Ultimate Crack + Portable (2025)

C union

  • 2. Defination & introduction • Unions are con conceptually similar to structures • The syntax of union is also similar to that of structure • The only difference is in term of storage • In structure each member has its own storage location where the all members of union uses a single shared memory location which is equal to the size of its largest data member . • Data can of int,char,float,double etc .data type • It is a user defined data type Note-> union does not support multiple value it can store only one value at a time •
  • 3. structure union Struct emp; union emp { { Char x; //size 1 byte char x; Float y;//size 4 byte float y; }e;}e; memory sharing x y x&y 5 byte 4 byte
  • 4. syntax Keyword→ union structure _ name { data_ type variable 1; Body data_type variable 2; ……………………………. data _type variable n; };
  • 5. example Union student { char name[200]; Int rollno; Float marks; }; 1.Here student is a the name of union
  • 6. Declaration of union variable method 1 Union student { Char name[200]; Int roll no; Float marks; }; Int main() { Union student student1; Return 0; }
  • 7. Declaration of union variable method 2 Union student { Char name[200]; Int rollno; Float marks; }student1; Int main() { Return 0; }
  • 8. Accessing the data members of union • The data member of union can be accessed as Union_variable.data_mamber Example The rollno of student then we can write as Student1.rollno
  • 9. Program of union• #include<stdio.h> • #include<string.h> • union student • { • char name[200]; • int rollno; • float marks; • • }student1; • int main() • { • • strcpy(student1.name,"ali"); • student1.rollno=201; • student1.marks=78.5; • printf("student name=%sn",student1.name); • printf("student roll no=%dn",student1.rollno); • printf("student marks=%fn",student1.marks); • return 0; • }
  • 10. Program of union• #include<stdio.h> • #include<string.h> • union student • { • char name[200]; • int rollno; • float marks; • • }student1; • int main() • { • • strcpy(student1.name,"ali"); • student1.rollno=201; • student1.marks=78.5; • printf("student name=%sn",student1.name); • printf("student roll no=%dn",student1.rollno); • printf("student marks=%fn",student1.marks); • return 0; • }