Question
Initialize and print the following union variables: 7, and 7.0 using the following program that has a union and a member to track what's in
Initialize and print the following union variables: 7, and 7.0 using the following program that has a union and a member to track what's in the union inside a structure.
#include
#include
/* code for types in union */
#define FLOAT_TYPE 1
#define CHAR_TYPE 2
#define INT_TYPE 3
struct S{
int type_in_union;
union{
float un_float;
char un_char;
int un_int;
}U;
}S;
void print_vt(void){
switch(S.type_in_union){
default:
printf("Unknown type in union ");
break;
case FLOAT_TYPE:
printf("%f ", S.U.un_float);
break;
case CHAR_TYPE:
printf("%c ", S.U.un_char);
break;
case INT_TYPE:
printf("%d ", S.U.un_int);
break;
}
}
int main(){
S.type_in_union = FLOAT_TYPE;
S.U.un_float = 3.5;
print_vt();
S.type_in_union = CHAR_TYPE;
S.U.un_char = 'a';
print_vt();
// Your Code
return 0;
}
Please solve the problem by writing in programming C language and use the existing code to solve the problem. Please explain what we are doing.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started