The printf() and the gets() are within the while
test, before the evaluation of option
.
In fact, with the current behavior you don't actually have to enter the number twice: You have to enter the number once and then type anything so gets() will return.
Edit: I added a few details on is how it runs:
- Instruction pointer reaches the very beginning of the
while
statement, begins evaluating its condition- calls printf()
- calls gets()
- evaluates
option < 0
, which is true.
- Body of the
while
statement- code parses string, sets
option
to a value above zero.
- code parses string, sets
- Instruction pointer is looped back to evaluating the condition
- calls printf()
- calls gets()
- evaluates
option < 0
, which is false.
while
loop terminates- calls
printf("something\n");
- calls
It's visible here that the buffer, once filled by gets(), is only read by the code in the first iteration, and is ignored on the second one.