4

I got this piece of code

#include<stdio.h>
#include <stdlib.h>

int main()
{
  int i;
  int number, S;
  float MO;


  S = 0;

  for(i=0;i<10;i++)
  {
    printf("AAA %d\n", i );
    scanf("%d\n", &number);
    S = S + number;
  }

  MO = S/10;

  printf("%d , %f \n",S , MO );
  return 0;
}

when the execution starts, AAA 0 is printed.I then give my first number.After that, i am expecting to see AAA 1 , but this will be printed only after i give my second number. Checked this here

C/C++ printf() before scanf() issue

but seems i can get none of these solutions work for me

Community
  • 1
  • 1

4 Answers4

4

The answers claiming that this has something to do with flushing input or output are wrong. The problem has nothing to do with this. The appearance of the \n character at the end of the scanf() template string instructs scanf() to match and discard whitespace characters. It will do so until a non-whitespace character is encountered, or end-of-file is reached. The relevant part of the C11 Standard is §7.21.6.2 5:

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.

In OP's case, a second number must be placed in the input stream so that the first can be assigned. The second number then remains in the input stream until the next call to scanf(). In the case of the example given by Stephan Lechner, taking input from a file, there is a number in the input stream after each number to be assigned, until the last number (if there are exactly ten numbers), and then the EOF causes scanf() to return. Note that OP could also have signalled EOF from the keyboard after each input. Or, OP could enter all numbers on one line, with an extra number to signal end of input:

1 2 3 4 5 6 7 8 9 10 11

The solution is simply to remove the \n from the end of the scanf() template string. Whitespace characters at the end of such a template string are tricky, and almost never what is actually desired.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
3

Just remove the \n from the scanf format string:

scanf("%d", &number);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    Why is this necesssary? If he presses Return after the number, won't that match the format string? – Barmar Jan 26 '17 at 21:34
  • 1
    @Barmar No, the `\n` will match any amount of whitespace after the number. So it could match one newline or a hundred (and any number of spaces or tabs as well). The `scanf` won't return until it sees at least one non-whitespace character. And it won't see that non-whitespace character until you type a non-whitespace character and press enter. – user3386109 Jan 26 '17 at 21:37
  • 2
    That should be explained in the answer. – Barmar Jan 26 '17 at 21:40
  • I answered a similar question a couple of weeks ago, regarding space in `scanf()`: http://stackoverflow.com/questions/41534295/c-programming-role-of-spaces-in-scanf/41534477#41534477 I didn't realize `\n` acted the same way. – Barmar Jan 26 '17 at 21:41
  • 1
    @Barmar Yup, it's easy to miss, but there's a little note buried in the man page that says, *"White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input."* So it's a common mistake to expect a `\n` in the format to match a newline character, but in fact it's no different than just putting a space in the format string. – user3386109 Jan 26 '17 at 21:47
3

"\n" waits for non-white-space. Instead use scanf("%d", &number) @Michael Walz and check its return value.

Let us break down scanf("%d\n", &number);

  1. "%d" Scan for numeric text. This specifier will allow leading white-space. Once some non-numeric character is found, put that character back into stdin. If valid numeric text for an integer was found, convert to an int and save to number.

  2. "\n" Scan and discard 0 or more white-spaces such as '\n', ' ', and others. Continue scanning until non-white-space is encountered, put that character back into stdin.

Now return the number of fields scanned (1).


Notice step 2. User had to type in data after the Return or '\n' to get scanf() to return.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

The issue is with the buffering of stdout which doesn't force a flush unless a \n is in the output or fflush is called specifically

levengli
  • 1,091
  • 7
  • 18