mercredi 12 juin 2019

Output of union using if statement

Hey I have a homework problem on this c code:

#include<stdio.h>

typedef union{
char var1;
int var2;
float var3;
}data;

int main()
{
data mydata;

mydata.var1 = 'B';
mydata.var2 = 12;

if(mydata.var1 == 'B')
    mydata.var3 = 3.5;
else
    mydata.var3 = 7.1;

printf("%.1f", mydata.var3);
return 0;
}


The output is 7.1, i was wondering if someone could explain why the output is 7.1 and not 3.5.

Cheers for your help.

Aucun commentaire:

Enregistrer un commentaire