TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Friday 15 May 2020

Union In C Language | Union In C In Hindi | Union In C Programming | Union In C Example

Union In C


 Union in c language is a user-defined data type that is used to store the different type of elements.

 Only one member of the union can occupy the memory. In other words, we can say that the size of the union in any instance is equal to the size of its largest element.

image of union in c language

Fig: Union In C

Advantage of union over structure


It occupies less memory because it occupies the size of the largest member only.


Disadvantage of union over structure


Only the last entered data can be stored in the union. It overwrites the data previously stored in the union.


Defining union


The union keyword is used to define the unionunion union_name .    

      

      Syntax:
{  

    data_type member1;  

    data_type member2;  

    .  

    .  

    data_type memeberN;  

};  

 

Example:

 

union employee  

{   int id;  

    char name[50];  

    float salary;  

};  

 

Program


#include <stdio.h>  

#include <string.h>  

union employee    

{   int id;    

    char name[50];    

}e1

int main( )  

{  

   

   e1.id=101;  

   strcpy(e1.name, "Pramod Dwivedi");

  

 printf( "employee 1 id : %d\n", e1.id);  

 

   printf( "employee 1 name : %s\n", e1.name);  

 

   return 0;  

 

}  


Output:


employee 1 id : 101

employee 1 name : Pramod Dwivedi

No comments:

Post a Comment