-->
Showing posts with label program to initialize union. Show all posts
Showing posts with label program to initialize union. Show all posts

program to initialize union

using codeblocks
----------------------------------------------------------------------------------

This does not work!
---------------------------------

//initializing  union with some data
#include <stdio.h>                         //header file
#include<string.h>
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc={"rajan","KTM"};                   //initializing data
int main()
{
    printf("the name is ");
    puts(acc.name);                    //puts data on screen i.e. name
    printf("the address is\n");
    puts(acc.address);                  //puts data on screen but does not get displayed on screen
    return 0;
}
// we have tried to access both members at same time.
//so, We get same data "rajan" two times. We do not get "KTM".
----------------------------------------------------------------------------------------------------------------

This works!
-------------------------------------------------------

//initializing  union with some data
#include <stdio.h>                         //header file
#include<string.h>                         //handles strings
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;                                //data variable
int main()
{
    strcpy(acc.name,"Rajan");         //data initialization and being copied to 'name'
    printf("the name is ");
    puts(acc.name);                    //puts data on screen i.e. name
    strcpy(acc.address,"KTM");         //data initialization and being copied to 'address'
    printf("the address is\n");
    puts(acc.address);                  //puts data on screen but does not get displayed on screen
    return 0;
}
// we have  accessed one member at a  time.
//so, We get different data "rajan" then ktm.
---------------------------------------------------------------------------------------------------------------
using turbo c++
----------------------------------------------------------------------------------------------
//initializing  union with some data
#include <stdio.h>                         //header file
#include<string.h>                         //handles strings
#include<conio.h>                        //header file
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;                                //data variable
int main()
{
    strcpy(acc.name,"Rajan");         //data initialization and being copied to 'name'
    printf("the name is ");
    puts(acc.name);                    //puts data on screen i.e. name
    strcpy(acc.address,"KTM");         //data initialization and being copied to 'address'
    printf("the address is\n");
    puts(acc.address);                  //puts data on screen but does not get displayed on screen
    getch();
     return 0;
}
// we have  accessed one member at a  time.
//so, We get different data "rajan" then ktm.