SlideShare a Scribd company logo
1 of 10
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

What's hot (20)

Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Structure in c
Structure in cStructure in c
Structure in c
 
Unions in c
Unions in cUnions in c
Unions in c
 
5bit field
5bit field5bit field
5bit field
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c language
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Thread
ThreadThread
Thread
 
File access methods.54
File access methods.54File access methods.54
File access methods.54
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Enums in c
Enums in cEnums in c
Enums in c
 
Data types
Data typesData types
Data types
 
Buffering.pptx
Buffering.pptxBuffering.pptx
Buffering.pptx
 
Typedef
TypedefTypedef
Typedef
 

Similar to C union definition and introduction

Structures unions
Structures  unionsStructures  unions
Structures unionsSugnan M
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data StruturesAcad
 
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxvekariyakashyap
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxSKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxLECO9
 
Union
UnionUnion
UnionAcad
 
Union in c language
Union in c languageUnion in c language
Union in c languageTanmay Modi
 
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...ssuser5610081
 
Engineering Computers - L29-Structures.pptx
Engineering Computers - L29-Structures.pptxEngineering Computers - L29-Structures.pptx
Engineering Computers - L29-Structures.pptxhappycocoman
 
Constant a,variables and data types
Constant a,variables and data typesConstant a,variables and data types
Constant a,variables and data typesmithilesh kumar
 

Similar to C union definition and introduction (20)

UNION 1.pptx
UNION 1.pptxUNION 1.pptx
UNION 1.pptx
 
Structures unions
Structures  unionsStructures  unions
Structures unions
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data Strutures
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptxUnit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
Unit_6StructureandUnionpptx__2023_01_04_16_48_56.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
Union
UnionUnion
Union
 
Union in c language
Union in c languageUnion in c language
Union in c language
 
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
Data Types in C++-Primary or Built-in or Fundamental data type Derived data t...
 
Union
UnionUnion
Union
 
C- language Lecture 7
C- language Lecture 7C- language Lecture 7
C- language Lecture 7
 
Unions
UnionsUnions
Unions
 
Engineering Computers - L29-Structures.pptx
Engineering Computers - L29-Structures.pptxEngineering Computers - L29-Structures.pptx
Engineering Computers - L29-Structures.pptx
 
Csc240 -lecture_4
Csc240  -lecture_4Csc240  -lecture_4
Csc240 -lecture_4
 
Constant a,variables and data types
Constant a,variables and data typesConstant a,variables and data types
Constant a,variables and data types
 
Types in .net
Types in .netTypes in .net
Types in .net
 
1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 

Recently uploaded

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 

C union definition and introduction

  • 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; • }