c - The "EOF" value in different type (an union)? -
i reading c book k&r. know the macro eof
on system -1
.
int eof = eof; printf("eof on system is: %d\n",eof);
but when assign eof
union, output somehow confused me. can understand v.ival
-1
. can explain why rest of them: v.cval
, v.fval
, , v.dval
this?
union eof_val { int ival; char cval; float fval; double dval; } eof; eof.ival = eof; printf("eof int = %d\n", eof.ival); /* output: -1 */ printf("eof char = %c\n", eof.cval); /* output: � */ printf("eof float = %f\n", eof.fval); /* output: nan */ printf("eof double = %f\n", eof.dval); /* output: 0.000000 */
a union big enough hold biggest of it's members. not how them all. 1 of it's values usable @ time.
you've set ival
, it's good, means cval
, fval
, dval
pretty hold rubbish (not quite: figure out based on value of ival
)
not duplicate, interesting reading: why need c unions?
Comments
Post a Comment