简单的C程序

发布于 2024-08-06 06:06:11 字数 516 浏览 5 评论 0原文

好的,我正在尝试学习 C,我希望我的用户输入一个值,所以我使用 scanf。我一开始没有冲洗,因为直到我输入两个值之前什么也没有发生。现在我有了它们,尽管我遇到了同样的问题,但在我输入两个数字之前仍然没有输出。这是我的代码:

#include <stdio.h>
using namespace std;

int main()
{

    int i1, i2, sums;

    printf( "Enter first integer\n" );
    fflush(stdout);
    scanf( "%d", &i1 );

    printf( "Enter second integer\n" );
    fflush(stdout);
    scanf( "%d", &i2 );
    sums = i1 + i2;

    printf( "Sum is %d\n", sums );
    fflush(stdout);
    return 0;
}

任何帮助将不胜感激。

Ok so I am trying to learn C and I want my user to input a value so I am using scanf. I started off not having the flushes, because nothing was comming up until I typed in two values. Now that I have them though I get the same problem there still is no output until after I type in two numbers. Here is my code:

#include <stdio.h>
using namespace std;

int main()
{

    int i1, i2, sums;

    printf( "Enter first integer\n" );
    fflush(stdout);
    scanf( "%d", &i1 );

    printf( "Enter second integer\n" );
    fflush(stdout);
    scanf( "%d", &i2 );
    sums = i1 + i2;

    printf( "Sum is %d\n", sums );
    fflush(stdout);
    return 0;
}

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-08-13 06:06:11

以下对我来说效果很好:

#include <stdio.h>
int main() {
    int i1, i2, sums;

    printf( "Enter first integer\n" );
    scanf( "%d", &i1 );

    printf( "Enter second integer\n" );
    scanf( "%d", &i2 );

    sums = i1 + i2;
    printf( "Sum is %d\n", sums );

    return 0;
}

并给出:

Enter first integer
1
Enter second integer
6
Sum is 7

这是在 XP 下使用 Cygwin。您使用什么平台和编译器?

更新:一种可能性是,因为您是在 Eclipse 环境中运行,所以它可能会做一些奇怪的事情,干扰正常的 I/O 规则。

我非常确定,即使 stdout 不像 stderr 那样进行行缓冲,如果您尝试从 stdin 读取(至少在我使用过的大多数环境中,这是少数),它也会自动刷新。

Eclipse 可能正在调整将控制台连接到程序实际 I/O 的方式。我会尝试将代码编译为独立的可执行文件,然后在 Eclipse 环境之外运行它。如果在那里运行正常,那么可能是 Eclipse 和程序之间的交互。

正如我所说,即使没有刷新,您的程序也可以在带有 Cygwin 的 XP 下正常运行。

需要进一步解释。正如 Jerry Coffin 在评论中正确指出的那样,C 标准(c1x,2009/03/01 草案)指出:

5.1.2.1第6段:交互设备的输入和输出动态应按照7.19.3的规定进行。这些要求的目的是尽快出现无缓冲或行缓冲的输出,以确保提示消息实际出现在程序等待输入之前。

5.1.2.1 para 7:交互设备的构成是由实现定义的。

7.9.13 para 3:当流未缓冲时,字符应尽快从源或目标出现。否则,字符可能会作为块累积并传输到主机环境或从主机环境传输。当流完全缓冲时,当缓冲区已满时,字符将作为块传输到主机环境或从主机环境传输。当流是行缓冲时,当遇到换行符时,字符将作为块传输到主机环境或从主机环境传输。

7.9.13 para 7:在程序启动时,预定义了三个文本流,无需显式打开 - 标准输入(用于读取常规输入)、标准输出(用于写入常规输出)、和标准错误(用于写入诊断输出)。最初打开时,标准错误流未完全缓冲;当且仅当可以确定该流不引用交互设备时,标准输入和标准输出流才被完全缓冲。

可能发生的情况是 Eclipse 与程序输入和输出交互的方式可能导致程序无法将 stdout 识别为交互设备。然后它将被完全缓冲,这意味着在缓冲区已满或程序终止之前您不会看到输出。

The following works fine for me:

#include <stdio.h>
int main() {
    int i1, i2, sums;

    printf( "Enter first integer\n" );
    scanf( "%d", &i1 );

    printf( "Enter second integer\n" );
    scanf( "%d", &i2 );

    sums = i1 + i2;
    printf( "Sum is %d\n", sums );

    return 0;
}

and gives:

Enter first integer
1
Enter second integer
6
Sum is 7

This is using Cygwin under XP. What platform and compiler are you using?

Update: One possibility is that, because you're running from within the Eclipse environment, it may be doing some weird stuff that interferes with the normal I/O rules.

I'm pretty certain that stdout, even if it's not line buffered like stderr, will autoflush if you attempt to read from stdin (at least in most environments I've used, which is a few).

Eclipse may be fiddling around with the way it attaches the console to the program's actual I/O. I would try to compile the code to a standalone executable and then run it outside the Eclipse environment. If it runs fine there, then it's probably the interaction between Eclipse and the program.

As I stated, your program works fine under XP with Cygwin, even without the flushes.

Further explanation is warranted. As Jerry Coffin rightly points out in a comment, the C standard (c1x, 2009/03/01 draft) states:

5.1.2.1 para 6: The input and output dynamics of interactive devices shall take place as specified in 7.19.3. The intent of these requirements is that unbuffered or line-buffered output appear as soon as possible, to ensure that prompting messages actually appear prior to a program waiting for input.

5.1.2.1 para 7: What constitutes an interactive device is implementation-defined.

7.9.13 para 3: When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

7.9.13 para 7: At program startup, three text streams are predefined and need not be opened explicitly - standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.

What may be happening is that the way Eclipse interacts with the programs input and output may be causing the program to not recognize stdout as an interactive device. It would then be fully buffered, meaning that you wouldn't see the output until the buffer is full, or the program terminates.

活雷疯 2024-08-13 06:06:11

我认为你需要在“scanf”旁边添加“\n”。像这样。

scanf( "%d\n", &i2 );

尝试一下。

I think you need "\n" in side the "scanf". Like this.

scanf( "%d\n", &i2 );

Try it.

月隐月明月朦胧 2024-08-13 06:06:11

我认为你正在冲洗错误的东西,尝试使用 stdin 冲洗 scanf 而不是使用 stdout,就像这样

#include

main()
{
int i、j、总和;
printf("请输入第一个整​​数\n");

  scanf("%d",&i);
  fflush(stdin);
  printf("enter the second integer\n");

  scanf("%d",&j);
  fflush(stdin);
  sums = i + j;
  printf("sum is %d\n",sums);
  //fflush(stdin);
  getchar();

}

i think your are flushing out the wrong stuff , try flushing scanf using stdin not using stdout,just like this

#include

main()
{
int i, j,sums;
printf("enter the first integer\n");

  scanf("%d",&i);
  fflush(stdin);
  printf("enter the second integer\n");

  scanf("%d",&j);
  fflush(stdin);
  sums = i + j;
  printf("sum is %d\n",sums);
  //fflush(stdin);
  getchar();

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文