What i Gotta DO:
Your solution must include a function called displayUnion() which will take a integer_union variable as a parameter. The function will have simply 4 printf statements, and nothing else! This function will need to be invoked 4 times from the main() program.
Create a union called "integer_union" with members char c, short s, int i and long l. Each union variable should be printed as a char, a short, an int and a long.
char: w
short: 1234
int: 1234567
long: 1234567890
What i got:
Code:#include<stdio.h>
union integer_union{
char c;
short s;
int i;
long l;
};
int main()
{
union integer_union value;
printf( "Please enter a char: " );
scanf( "%c", &value.c, "%hd", value.s, "%d", &value.i, "%ld", &value.l );
printf( "char c = %c\nshort s = %hd\nint i = %d\nlong l = %ld", value.c, value.s, value.i, value.l );
printf( "\n\nPlease enter a short: ");
scanf( "%hd", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
printf( "char c = %c\nshort s = %hd\nint i = %d\nlong l = %ld", value.c, value.s, value.i, value.l );
printf( "\n\nPlease enter a int: ");
scanf( "%d", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
printf( "char c = %c\nshort s = %hd\nint i = %d\nlong l = %ld", value.c, value.s, value.i, value.l );
printf( "\n\nPlease enter a long: ");
scanf( "%ld", &value.c, "%hd", &value.s, "%d", &value.i, "%ld", &value.l );
printf( "char c = %c\nshort s = %hd\nint i = %d\nlong l = %ld\n\n", value.c, value.s, value.i, value.l );
return 0;
}
It works but im not sure if this is the right way , not sure bout the function called displayUnion() how to implement it er what?