printf不能在日食的控制台上打印吗? [英] printf not print on the console in eclipse?

查看:61
本文介绍了printf不能在日食的控制台上打印吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

int main() {
    int n, s, i;
    do {
        printf("n= "); // here is the problem ?
        scanf("%d", &n);
    } while (n<100 || n <= 0);
    s = 0;
    i = 0;
    while (i <= n) {
        i = i + 2;
        s = s + i;
    }
    printf("s=%d", s);
    getchar();
    return 0;
}

我在eclipse c / c ++中运行它,但未打印 n =第一。但是,当我在DEV-C ++或VS 2017等其他IDE中运行它时,它运行良好。当在printf之后添加此行时,我按预期运行。

I ran it in eclipse c/c++ and it not print "n=" first. But when I run it in another IDE like DEV-C++ or VS 2017, it run well. When add this line after printf and I ran like I expected.

fflush(stdout);

这是什么问题?

推荐答案

printf 不会刷新到屏幕,除非刷新缓冲区



好像您的流被缓冲了。写入 stdout 和其他流的数据将被缓冲,并且在刷新缓冲区后将全部输出。由于IO在所有CPU操作中最慢,因此可以实现更好的性能。

printf doesn't print to screen unless buffer is flushed

Looks like your streams are buffered. Data you write to stdout and other streams is buffered and all output once you flush your buffer. This allows for better performance as IO is slowest among all your CPU operations.

目前,您至少有以下选择:

At this point, you have at least these options:


  1. 每次使用 printf fflush(stdout)显式刷新缓冲区c $ c>

  2. 禁用缓冲 setbuf(stdout,NULL);

  3. 刷新缓冲通过在 printf 字符串末尾使用换行符 \n 例如: printf( n = \n);

  1. Explicitly flush the buffer by calling fflush( stdout ) every time you use printf
  2. Disable buffering setbuf(stdout, NULL);
  3. Flush buffer by using newline \n at end of printf string Ex: printf("n= \n");

您的代码在某些环境中有效,可能是因为那里禁用了缓冲。

Your code worked in some environments probably because buffering is disabled there.

这篇关于printf不能在日食的控制台上打印吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆