I am reading the C book K&R. I know that the macro EOF
on my system is -1
.
int eof = EOF;
printf("EOF on my system is: %d\n",eof);
But when I assign EOF
to a union, the output somehow confused me. I can only understand v.ival
is -1
. Anyone can explain why the rest of them: v.cval
, v.fval
, and v.dval
would be like this?
union eof_val {
int ival;
char cval;
float fval;
double dval;
} eof;
eof.ival = EOF;
printf("EOF to int = %d\n", eof.ival); /* output: -1 */
printf("EOF to char = %c\n", eof.cval); /* output: � */
printf("EOF to float = %f\n", eof.fval); /* output: nan */
printf("EOF to double = %f\n", eof.dval); /* output: 0.000000 */