2

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Maths Maniac
  • 83
  • 1
  • 7

1 Answers1

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Thanks for ur help,but i am not getting it,can u please explain in a simple way.I just inputted a number and pressed enter but it is not printing the number i have inputted. – Maths Maniac Jul 29 '17 at 21:51
  • 3
    @MathsManiac: Of course it is still waiting. It will continue to wait until you type something that isn't a white space after the number. You could type a letter or a digit or a punctuation character — but typing blanks, tabs or newlines will just leave `scanf()` looking for something else. Remove the trailing newline from the input format — such trailing white space is unconditionally a disaster for interactive I/O and seldom if ever wise for non-interactive I/O. If you want to read lines of input, do so — using `fgets()` or POSIX `getline()` — and then parse what was read. – Jonathan Leffler Jul 29 '17 at 21:54