Menu

UNION in C

  • UNIONS like structures contain members whose individual data types may vary.
  • The syntax of UNION is similar to structure.
  • There is a major distinction between structure and union in terms of storage. ie, in structure each member has its own storage location,whereas all the members of the union use the same location.
  • union is declared using the keyword 'union'.

Syntax

     union union_name{
        datatype member1;
        datatype membe2;
        .................
        .................
        datatype membern;
     }

Example

      #include<stdio.h>
      main(){
        union number
        {
           int one;
           char two;
        };
        union number val;
        val.one=300;
        printf("val.one=%d",val.one);
        printf("val.two=%d",val.two);
      }

Output

           value.one=300
           value.two=44