I have this code :
char c;
do
{
scanf("%c",&c);
printf("coucou\n");
} while (c!='q');
And here my result :
Input:
M
Output:
coucou
coucou
Why does it print "coucou" 2 times every time ?
The cycle is executed 2 times because scanf
scans 2 characters, one for M
and another for the \\n
new line character.
You can easily solve this by adding a condition to your cycle:
do
{
if(getchar() != '\n')
printf("coucou\n");
} while (c!='q');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.