Q37 Write a C program to declare, initialize an union, example of union.

Admin

Write a C program to declare, initialize an union, example of union. 

#include <stdio.h>
union pack{
char a;
int b;
double c;
};
int main()
{
    union pack p;
    printf("\nOccupied size by union pack: %d",
                sizeof(p));
    p.a='A';
    printf("\nValue of a:%c",p.a);
    p.b=10;
    printf("\nValue of b:%d",p.b);
    p.c=12345.6790;
    printf("\nValue of c:%f",p.c);
    p.a='A';
    p.b=10;
    p.c=12345.6790;
    printf("\nValue of a:%c, b:%d, c:%f",p.a,p.b,
          p.c);
    return 0;
}

Output

Occupied size by union pack: 8
Value of a:A
Value of b:10
Value of c:12345.679000
Value of a:■, b:-377957122, c:12345.679000

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.