简体   繁体   中英

C printf wont print before scaning next number

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

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.

只需从scanf格式字符串中删除\\n

scanf("%d", &number);

"\\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.

问题在于stdout的缓冲,除非输出中有\\n或者特定地调用fflush否则不会强制刷新

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM