Newline character(\\n) in scanf [duplicate]

2019-07-29 04:32发布

问题:

This question already has an answer here:

  • Why does scanf ask twice for input when there's a newline at the end of the format string? 6 answers

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.