0
Follow
0
View

What if there are zeros in scanf?(Language-C language)

denise222 注册会员
2023-02-27 13:24

https://baike.baidu.com/item/scanf/10773316?fr=aladdin
scanf("%d0",& x); The function reads the first %d, which is an integer, and the next '0' remains in the input buffer, waiting for the next read.

eee123lhm 注册会员
2023-02-27 13:23

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > if you execute the following program from the keyboard input 5, when the output is undefined.

This is because %d0 in the format string instructs the scanf function to read an integer and ignores the 0 after the input value when reading the integer. So if you enter the number 5, the scanf function will try to interpret it as an integer, and then continue reading the input until it finds a non-numeric character. In this case, it reads and ignores the 0 after the input value, and continues to wait for more input.

However, because the scanf function does not automatically clear all remaining input values from the keyboard buffer, if you continue typing other characters(such as the enter key) after entering 5, they will remain in the keyboard buffer for the next input operation.

Therefore, it is recommended to use %d instead of %d0 to make it clearer that you are reading integers.

hainancyp 注册会员
2023-02-27 13:23

This answer quotes ChatGPT

In C, %d is a format controller used to read the value of a variable of type integer. When a 0 is followed by the format control character, it means reading an integer and multiplying it by 10.

What %d0 does in the code you provide is read an integer and multiply it by 10. So if you type 5 from the keyboard, the program will multiply it by 10 to get 50, and assign it to the variable x.

Note that the semicolon in the code should be a typo and should be placed at the end of the statement rather than at the end of the formatted string. Therefore, the correct code would be:


scanf("%d0", &x);

This reads an integer and multiplies it by 10, then stores it in the variable x.

About the Author

Question Info

Publish Time
2023-02-27 13:23
Update Time
2023-02-27 13:23