This is a C program to find the size of a Union.
Problem Description
This Program finds the size of a Union.
Problem Solution
1. Define the union.
2. Finds its size using keyword sizeof().
3. Print the same and exit.
Program/Source Code
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);
}
Program Explanation
1. Define the union named sample.
2. Declare three variables m, n and ch of different data types.
3. Use the keyword sizeof() to find the size of a union and print the same.
4. Initialize each variable with some value and print its value as output.
5. Exit.
advertisement
advertisement
Runtime Test Cases
The size of union = 4 25 0.000000 1045220557 0.200000 1045220464 0.199999
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics of C, go to C Programming Examples.
Related Posts:
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Apply for C Internship
- Watch Advanced C Programming Videos