您的位置:首页 > 编程语言 > Java开发

eclipse中的关于scanf和printf的输入顺序的解决办法

2013-04-02 13:59 447 查看


eclipse中的关于scanf和printf的输入顺序的解决办法

The eclipse console has weird behaviour when used for input with C programs.

I teach C to first year undergraduates and I want them to learn their way through eclipse. But the small silly programs that ask you to input characters have weird behaviour, if you use the
eclipse console. It seems like it groups all input and output commands and executes them together...for example...

the following program:

#include <stdio.h>

int main() {

int n = 0;

printf("Gimme a number: ");

scanf ("%d", &n);

printf("/nThe number you entered was %d/n", n);

    return 0;

}

has the following output:

4

Gimme a number: The number you entered was 4

Pretty normal output on any console you'll find, not just with eclipse's one

  

instead of:

Gimme a number: 4

The number you entered was 4

To obtain this output, you have to flush stdout before scanf'ing the number. The output is flushed either implicitely when a newline character is echoed on the console (printf("/n")) or explicitely with fflush(stdout);

to get the output you wanted use this program :

#include <stdio.h>

int main()  {

    int n = 0;

    printf("Gimme a number: ");

    fflush(stdout);

    scanf ("%d", &n);

    printf("/nThe number you entered was %d/n", n);

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Eclipse debug
相关文章推荐