[c language] special case of scanf() function

Essays on reviewing c language

the first case

When using scanf, there will be a phenomenon that scanf() is skipped, as follows:

 

Here, the printf() statement is executed without letting the input c

reason:

When scanf reads integers, floating-point numbers, and strings, it ignores symbols such as '\n' and space characters.

After inputting 100 of i, 100\n is in the buffer, and the first scanf reads 100, leaving '\n', which causes the second scanf to read the buffer and finds that there is a newline character in it, so there is no Let the input, but directly read the newline character

Solution:

In lower versions of vs, you can use::

fflush(stdin);

In higher versions of vs, fflush() is replaced by rewind() and should be used:

rewind(stdin);

Clear the data in the buffer when it works, and you can remove the '\n' newline character here

the second case

When outputting, the character c is missing. Here, scanf matches the space after 100 to c when matching, causing c to become a space, and subsequent matching errors

Solution:

 Add a space between %d and %c in scanf

Guess you like

Origin blog.csdn.net/Wuyeyu2001/article/details/127299540