This is just a really simple do-while loop. What I am curious about is what happens when you enter a non-integer number. For example if you enter a character, say 'r' from the keyboard. If you do this at the first call, it printsCode:#include <stdio.h> int main() { int x; do { scanf("%d",&x); printf("The number is %d\n",x); } while (x != 0); }
and then exits, which just means that x happened to be 0 already. If x wasn't 0, or if you enter a valid number first, say 4 on the first iteration, and then 'r' on the next, it will go into an infinite loop and printCode:The number is 0
forever.Code:The number is 4 The number is 4 The number is 4 The number is 4 The number is 4 . . .
What exactly is happening? Why does it skip the scanf and not allow another entry when a non-integer character is input? I suspect that I am in undefined behaviour land here, but does anyone know?