c - increment in while loop doesn't work? -
i doing following while loop, , line number doesn't incremented (it's 0). why that?
int main(int argc, const char* argv[]) { int line_number = 0; int f = 0; while (f == 0) { printf("line number %f\n", line_number); line_number++; } return exit_success; }
(i realize infinite loop, interested here in why line_number isn't getting incremented.)
with printf
, matters letter put after %
sign variable replacement. if @ the list of format specifiers, you’ll see %f
floating-point numbers. means thinks line_number
float; doesn’t proper conversion because doesn’t realize needs to, , instead reads same bits float.
the way floating-point format works complicated, you’ll stay close 0 long time. depending on how fast computer , how patient are, might or might not see change if leave running longer.
(technically it’s worse this, undefined behavior; however, outcome.)
most compilers have option warn if use wrong format specifier; should turn on default set of compiler warnings can tell , other issues.
Comments
Post a Comment