Newline character(\n) in scanf [duplicate]

谁都会走 提交于 2019-12-24 10:10:04

问题


Suppose i write the code below:

#include<stdio.h>
int main()
{
    int a,b;
    scanf("%d\n",&a);
    printf("%d",a);
    return 0;
}

The input is taken and the cursor is blinking in the next line without printing the value of a.
But if I remove the \n character, it is printing the value of a automatically in the next line.
Even if I place a \n, before the %d in scanf (scanf("%d\n",&a);), it is not moving the cursor to the next line, and taking the input, instead of being taking input in the next line. So, does scanf automatically takes input in next lines? and does \n cannot be used with the scanf function??

Actually, my problem wants me to input three integers in three line. It is written Input: Three integers on three lines.
But on trying to use \n in scanf, it is only showing a cursor blinking in the next line after taking the input.


回答1:


Any whitespace-character (as determined by isspace()) in the format-string for scanf() will cause it to read and discard characters until the next character read would be non-whitespace, or an error occurs.

You didn't enter any other non-whitespace than the number? Well, have fun waiting.



来源:https://stackoverflow.com/questions/45394221/newline-character-n-in-scanf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!