zoukankan      html  css  js  c++  java
  • 今天发现Eclipse cdt 使用scanf后的一个BUG

    今天发现Eclipse的一个BUG今天发现Eclipse cdt 使用scanf后的一个BUG

    就是在输入函数scanf后,发现前面的东西都不能打印在console里边,只有先输入东西以后才行,上网搜索以后发现一个帖子。

    Nikos Panagos wrote:

    Hello everybody,


    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;

    }

     

    a very good example program to teach students about buffered streams :)

     

     

    大意就是要在打印语句后加个fflushstdout)才行,所以我加了。就可以输入了,程序就正确了。

  • 相关阅读:
    Spring框架之 我对AOP的理解
    第二次分班考试之 ---纠错19/25题
    Spring IOC(控制反转) 和DI
    一级缓存,二级缓存
    多对多连接
    MyBatis 智能标签
    小结javaScriptOOP的对象内容点
    15年错题小结2月
    《Java周边》Http请求模拟工具(postman)
    《Java周边》IDEA 设置快捷键和快捷键中英文对照
  • 原文地址:https://www.cnblogs.com/nickchan/p/3104544.html
Copyright © 2011-2022 走看看