-->

program show use of union with input and output.

using codeblocks
-----------------------------------------------------------------------------------------------------------
//understanding union with simple input and output
#include <stdio.h>                         //header file
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;
int main()
{
    printf("enter name\n");
    gets(acc.name);                    //gets input for name
    printf("the name is ");
    puts(acc.name);                    //puts data on screen
    printf("enter address\n");
    gets(acc.address);                  //gets address
    printf("the address is ");
    puts(acc.address);                  //puts data on screen
    return 0;
}

Note:-
// we can input one member at a time so, We have input first member only then second member and so on.
// If we input input all members at same time, It would get crashed and give unexpected result
------------------------------------------------------------------------------------------------------
using turbo c++
--------------------------------------------------------------------------------------------------------
//understanding union with simple input and output
#include <stdio.h>                         //header file
#include<conio.h>                      //header file to hold output
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;
int main()
{
    printf("enter name\n");
    gets(acc.name);                    //gets input for name
    printf("the name is ");
    puts(acc.name);                    //puts data on screen
    printf("enter address\n");
    gets(acc.address);                  //gets address
    printf("the address is ");
    puts(acc.address);                  //puts data on screen
    getch();                                  //holds output
    return 0;
}
// we can input one member at a time so, We have input first member only then second member and so on.
// If we input input all members at same time, It would get crashed and give unexpected result


No comments:

Post a Comment