Use of Union in c++


    #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<<"\nb = "<<s.b ;
        /* stored as float and retrieved as float */
        s.c = 100.34 ;
        cout<<"\nc = "<< s.c;
        /*stored as float and retrieved as integer-a wrong way*/
        s.c = 1.567 ;
        cout<<"\na = "<<s.a  ;
        getch () ;
    }

0 comments:

Post a Comment

 
 
 
 


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