I'm working on program which input looks as follows:
3.14 (it's variable stored in union)
4 (number of calls)
int (asked types to return)
long
float
double
On output should i get:
1078523331
1078523331
3.140000
0.000000
Full instruction to this task
My program works except on double case: instead of giving me any output program gives me none. Can anyone explain me why? Here is my code.
#include <stdio.h>
#include <string.h>
#define SIZE 1000
#define CHARLENGTH 6
union Data {
int i;
long long l;
float f;
double d;
};
int main(){
union Data x;
char types[SIZE][CHARLENGTH];
int n;
scanf("%f",&x.f);
scanf("%d",&n);
for(int i = 0;i<=n+1;i++){
fgets(types[i],CHARLENGTH,stdin);
types[i][strcspn(types[i],"\n")] ='\0';//removing newline
}
for(int i = 1;i<=n+1;i++){
if(strcmp(types[i], "int") == 0){
printf("%d\n",x.i);
}
else if(strcmp(types[i], "long") == 0){
printf("%lli\n",x.l);
}
else if(strcmp(types[i], "float") == 0){
printf("%f\n",x.f);
}
else if(strcmp(types[i], "double") == 0){
printf("%lf\n",x.d);
}
}
}