Program for Use of Union


    #include <iostream.h>
    #include <conio.h>
    void main ( )
    {
        union sample      /* union declaration */
        {
            int a ;
            char b ;
            float c ;
        } ;
        union sample s ;  /* union variable declaration */
        clrscr ( ) ;
        /* stored as integer and retrieved as integer */
        s.a = 10 ;
        cout<<"a  :  "<<s.a  ;

        /* stored as character and retrieved as character */
        s.b = 'x' ;
        cout<<"b  : "<<s.b<<endl ;
        /* stored as float and retrieved as float */
        s.c = 100.34 ;
        cout<<"c  : "<<s.c<<endl ;
        /*stored as float and retrieved as integer-a wrong way*/
        s.c = 1.567 ;
        cout<<"a : "<<s.a <<endl;
        getch ( ) ;
    }

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|