C语言 scanf() 问题之前的 C/C++ printf()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16877264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:32:51  来源:igfitidea点击:

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

cprintfoutputscanf

提问by quapka

I'm using Eclipse to code in C/C++ and I'm struggling with what might be something pretty easy. In my code below I use printf()and after scanf(). Althougth printfis written before scanf()the output differs. I was able to find out something about similar issue here. But I wasn't able to solve it. Any ideas?

我正在使用 Eclipse 用 C/C++ 编写代码,但我正在为可能非常简单的事情而苦苦挣扎。在我的代码下面我使用printf()之后scanf()。在输出不同printf之前写入althouth scanf()。我能够在这里找到有关类似问题的信息。但我无法解决它。有任何想法吗?

Code:

代码:

#include <stdio.h>

int main()
{
    int myvariable;

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}

Expected output:

预期输出:

Enter a number:1
1

Instead I get:

相反,我得到:

1
Enter a number:1

回答by zsawyer

Your output is being buffered. You have 4 options:

您的输出正在缓冲。您有 4 个选择:

  1. explicit flush

    fflushafter each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.

    fflush( stdout );
    
  2. have the buffer only buffer lines-wise

    useful for when you know that it is enough to print only complete lines

    setlinebuf(stdout);
    
  3. disable the buffer

    setbuf(stdout, NULL);
    
  4. disable buffering in your console through what ever options menu it provides

  1. 显式刷新

    fflush在每次写入后从缓冲区中获利并仍然明确地强制执行所需的行为/显示。

    fflush( stdout );
    
  2. 让缓冲区仅缓冲行

    当您知道仅打印完整行就足够时很有用

    setlinebuf(stdout);
    
  3. 禁用缓冲区

    setbuf(stdout, NULL);
    
  4. 通过它提供的任何选项菜单禁用控制台中的缓冲



Examples:

例子:

Here is your code with option 1:

这是带有选项 1 的代码:

#include <stdio.h>
int main() {

    int myvariable;

    printf("Enter a number:");
    fflush( stdout );
    scanf("%d", &myvariable);
    printf("%d", myvariable);
    fflush( stdout );

    return 0;
}


Here is 2:

这是2:

#include <stdio.h>
int main() {

    int myvariable;

    setlinebuf(stdout);    

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}


and 3:

和 3:

#include <stdio.h>
int main() {

    int myvariable;

    setbuf(stdout, NULL);     

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}

回答by quapka

Ok, so finally I used something similar to what @zsawyer wrote as an option labelled 3. In my code I inserted this line:

好的,最后我使用了类似于@zsawyer 所写的内容作为标记为 3 的选项。在我的代码中,我插入了这一行:

setvbuf(stdout, NULL, _IONBF, 0);

As a first line in main():

作为 main() 中的第一行:

#include <stdio.h>

int main()
{
    setvbuf(stdout, NULL, _IONBF, 0);

    int myvariable;

    printf("Enter a number:");
    scanf("%d", &myvariable);
    printf("%d", myvariable);

    return 0;
}

I got it from here.

我从这里得到的。

回答by George

Fast and painless, I've just defined a macro named "eprint"to add a flush()after calling printf(...)and I just use the eprint macro below:

快速而轻松,我刚刚定义了一个名为“eprint”的宏来在调用printf(...)之后添加一个flush( )并且我只使用下面的 eprint 宏:

#define eprintf(...) printf(__VA_ARGS__); \
                 fflush(stdout);      \


Sample code here:

示例代码在这里:

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

#define eprintf(...) printf(__VA_ARGS__); \
                     fflush(stdout);      \

int main(void) {
    int a;

    eprintf("a=");

    scanf("%d",&a);

    eprintf("I've read value %d.\n",a);

    return EXIT_SUCCESS;
}


Eclipse console output:

Eclipse 控制台输出:

a=5
I've read value 5.


PS: I've just wasted 30 minutes looking up this eclipse console issue and possible fixes, and this one seems to be the most straight forward and easy to understand for anyone looking for such a thing.

PS:我只是浪费了 30 分钟来查找这个 Eclipse 控制台问题和可能的修复方法,而对于任何寻找此类问题的人来说,这似乎是最直接且易于理解的。