Eclipse CDT 调用printf/cout 控制台(console)无输出

来源:互联网 发布:sql server 2015 64位 编辑:程序博客网 时间:2024/04/29 23:41

症状描述:

用Eclipse调试程序,执行printf和cout函数,但是console无内容显示。

 

原因分析:

Eclipse输出的内容是保存在buffer中,因此要显示相关内容,就必须刷buffer缓冲区。

 

解决方案:

1.在main函数开始时调用函数 setbuf(stdout,NULL);

2.在每个printf函数后调用函数 fflush(stdout);

 

int main(void) {
    setbuf(stdout, NULL);
    char* c="!!!Hello C!!!";
    printf(c); /* prints !!!Hello World!!! */
    //fflush(stdout);
    return EXIT_SUCCESS;
}
字符串c结尾没加\n,调试时报以下错误:
!!!Hello C!!!*stopped,reason="end-stepping-range",frame={addr="0x0040140f",func="main",args=[],file="..\src\HelloC.c",fullname="F:\\316\322\265\304\316\304\265\265\Workspaces\HelloC\Debug/..\src\HelloC.c",line="24"},thread-id="1",stopped-threads="all"

加上\n就好了。
int main(void) {
    setbuf(stdout, NULL);
    char* c="!!!Hello C!!!\n";
    printf(c); /* prints !!!Hello World!!! */
    //fflush(stdout);
    return EXIT_SUCCESS;
}

原创粉丝点击