Home > Back-end >  Why the scanf in the while (1) there will be a dead circulation outside, inside is not.
Why the scanf in the while (1) there will be a dead circulation outside, inside is not.

Time:04-08

# include
Int main ()
{
char c;
The scanf (" % c ", & amp; C);
While (1)
{
If (c=='#') break;
If (c>='A' & amp; & C<='z') c -=32;
Printf (" % c ", c);
}
return 0;
}

CodePudding user response:

CodePudding user response:

Not necessarily, if the start input # will break out of the loop, there will be no wireless loop,
If not input # at the beginning, so while there is no break condition, circulation ~ so will die

CodePudding user response:

The scanf (" % c ", & amp; C); In the while (1) {... } outside, read the keyboard input only once, if this time opportunity if (c=='#') break; Didn't grasp, infinite loop, of course, the while (1) is true forever, when the scanf (" % c ", & amp; C); On the while (1) inside the {}, will keep to read keyboard input, and then the if (c=='#') break; Judgment, have infinite opportunity withdrew,

CodePudding user response:

The loop condition is 1 because of you, forever is true, the scanf and outside loop, cause you didn't have a chance to enter again to update the value of c c
And the scanf in circulation, due to the scanf was blocked waiting for input, so every cycle you can re-enter c, so that you will have the opportunity to exit the circulation, such as input #
  • Related