C Union

C Union

union is keyword of c programming language. there are use to defined the user data type. we can use multiple data member within union. all union member is share the same memory location. In this reason only one member can be utilize those memory at a time. union provide an efficient way to use same memory location in multiple purpose.

Syntax

  union tag_name{
    data_member;
    data_member;
    //:
  }variable_list;
Iteam overview
union keyword
tag_name Name of union
data_member list of data type and its variables
variable_list union variable

Example

 //Books are name of union in this case
  union Books{
    int pages;//data member
    char book_name[25];//data member
  }book1,book2;//union variable
  

union are allocate memory of largest data member. and other data member are utilize this memory.

How to use union in c programming

Help of union variable we are use union in c programming. we can declare two type of union variable. first when we are declare union. for example

  union Book{
    //data member
  }book1,book2,book3;
  //here three Book union variable(book1,book2,book3)

And function parameter list and inside a function. for example.

/* 
  Example of union
*/
/*Header file*/
#include <stdio.h>
#include <string.h>
union Book{
  char book_name[30];
  int page;
};
//union variable in function parameter
void book_list(union Book mybook){
  //print book name
  printf("Book Name : %s\n",mybook.book_name);
}
int main(){
  /*
   inside function declare union book variable
  */
  union Book book1;
  strcpy(book1.book_name,"C Programming");
  book_list(book1);//passing union to function
  return 0;

}

Output

>Book Name : C Programming

we can use union pointer. for example

/* 
  Example of union pointer
*/
/*Header file*/
#include <stdio.h>
#include <string.h>
//union of book
union Book{
  char book_name[30];
  int page;
};
//union pointer in function parameter
void book_list(union Book *mybook){
  //print book name
  printf("Book Name : %s\n",mybook->book_name);
}
int main(){
  /*
   inside function declare union book variable
  */
  union Book book1,*book_ptr;
    
    book_ptr=&book1 ; //assign address
  
  strcpy(book1.book_name,"C Programming");
  
  book_list(&book1);//passing union pointer to function
  return 0;

}
Book Name : C Programming

Difference between union and structure

The most important difference of memory organization in union and struct. struct is allocate memory of all structure members. but union are allocated memory of largest data member in from of memory in bytes. for example

Strcut & Union
/* 
  Example of structure and union
*/
/*Header file*/
#include <stdio.h>

union union_student{
   char name[32];
   float marks;
}s1;
struct strcut_student{
   char name[32];
   float marks;
}s2;
int main(){
  //replace zu to d in windows compiler
  printf("size of union_student  : %zu\n",sizeof(s1));
  printf("size of strcut_student : %zu\n",sizeof(s2));
  return 0;

}
size of union_student  : 32
size of strcut_student : 36

© 2019, regularcodes.com, All rights reserved