Users Online
· Guests Online: 155
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
53 C Program to Find the Size of a Union
This C Program finds the size of a union. The program uses sizeof() keyword to get the size.
Here is source code of the C program to find the size of a Union. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the size of a union
*/
#include <stdio.h>
void main()
{
union sample
{
int m;
float n;
char ch;
};
union sample u;
printf("The size of union = %d\n", sizeof(u));
/* initialization */
u.m = 25;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.n = 0.2;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.ch = 'p';
printf("%d %f %c\n", u.m, u.n, u.ch);
}
$ cc pgm94.c
$ a.out
The size of union = 4
25 0.000000
1045220557 0.200000
1045220464 0.199999
Here is source code of the C program to find the size of a Union. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the size of a union
*/
#include <stdio.h>
void main()
{
union sample
{
int m;
float n;
char ch;
};
union sample u;
printf("The size of union = %d\n", sizeof(u));
/* initialization */
u.m = 25;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.n = 0.2;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.ch = 'p';
printf("%d %f %c\n", u.m, u.n, u.ch);
}
$ cc pgm94.c
$ a.out
The size of union = 4
25 0.000000
1045220557 0.200000
1045220464 0.199999
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.