Question: 10.8 (Using Unions) Create union integer with members char c, short s, int i and long b. Write a program that inputs values of type

10.8 (Using Unions) Create union integer with members char c, short s, int i and long b. Write a program that inputs values of type char, short, int and long and stores the values in union variables of type union integer. Each union variable should be printed as a char, a short, an int and a long. Do the values always print correctly?

#include

//Union integer definition

union integer

{

char c;

short s;

int i;

long b;

};

//Main function

int main(void)

{

// define union a

union integer a;

// prompt user to enter character from input device

printf("Enter a character: ");

// read character and put in union

scanf("%c", &a.c);

//Print the values of union

printf("%c printed as a character: %c ", a.c, a.c);

printf("%c printed as a short: %hd ", a.c, a.s);

printf("%c printed as an integer: %d ", a.c, a.i);

printf("%c printed as a long: %ld ", a.c, a.b);

//prompt user to enter a short value

printf(" Enter a short: ");

// read short and put in union

scanf("%hd", &a.s);

//Print eh values of union

printf("%c printed as a character: %c ", a.s, a.c);

printf("%c printed as a short: %hd ", a.s, a.s);

printf("%c printed as an integer: %d ", a.s, a.i);

printf("%c printed as a long: %ld ", a.s, a.b);

//prompt user to enter an integer value

printf(" Enter an integer: ");

// read integer and put in union

scanf("%d", &a.i);

//Print eh values of union

printf("%c printed as a character: %c ", a.i, a.c);

printf("%c printed as a short: %hd ", a.i, a.s);

printf("%c printed as an integer: %d ", a.i, a.i);

printf("%c printed as a long: %ld ", a.i, a.b);

//prompt user to enter an long value

printf(" Enter a long: ");

// read long and put in union

scanf("%ld", &a.b);

//Print eh values of union

printf("%c printed as a character: %c ", a.b, a.c);

printf("%c printed as a short: %hd ", a.b, a.s);

printf("%c printed as an integer: %d ", a.b, a.i);

printf("%c printed as a long: %ld ", a.b, a.b);

return 0;

}// end main

10.8 (Using Unions) Create union integer with members char c, short s,

My output for the first one is wrong

r as a short, integer, and long it should be 114

and 5 as an integer and long should be 5 but these are not showing correctly the others are.

can someone tell me why?

Select CAWINDOWS system32\cmd.exe Enter a character: r r printed s a character: r r printed as a short: -13198 r printed as an integer: -858993550 r printed as a long: -85899355e Enter a short: 5 E printed as a character: E E printed as a short: 5 E printed as an integer: -859045883 printed as a long : -859045883 Enter an integer: 12 printed as a character : E printed as a short: 12 E printed as an integer: 12 printed as a long : 12 Enter a long: 445316 printed as a character: printed as a short: -13436 printed as an integer: 445316 printed as a long: 445316 Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!