简单C程序 [英] Simple C Program

查看:129
本文介绍了简单C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我想学习C,我想让我的用户输入一个值,所以我使用scanf。我开始没有刷新,因为没有什么事情,直到我输入两个值。现在我有他们,虽然我得到相同的问题,仍然没有输出,直到我输入两个数字。这是我的代码:

  #include< stdio.h> 
using namespace std;

int main()
{

int i1,i2,sums;

printf(输入第一个整​​数\\\
);
fflush(stdout);
scanf(%d,& i1);

printf(输入第二个整数\\\
);
fflush(stdout);
scanf(%d,& i2);
sums = i1 + i2;

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

任何帮助将非常感激。

解决方案

以下适用于我:

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

printf(输入第一个整​​数\\\
);
scanf(%d,& i1);

printf(输入第二个整数\\\
);
scanf(%d,& i2);

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

return 0;
}

并提供:

 输入第一个整​​数
1
输入第二个整数
6
总和为7

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



更新:一种可能是,因为您从Eclipse环境中运行,做一些奇怪的东西,干扰正常的I / O规则。



我很确定stdout,即使它不像stderr行缓冲,将autoflush if你尝试从stdin读取(至少在大多数我使用的环境,这是几个)。



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



正如我所说,你的程序在XP下Cygwin工作正常,即使没有刷新。



进一步的解释是必要的。正如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 7 :在程序启动时,预定义了三个文本流,不需要明确打开 - 标准输入(用于读取常规输入),标准输出(用于写入常规输出)和标准错误输出)。最初打开时,标准错误流不完全缓冲;当且仅当可以确定流不指向交互式设备时,标准输入和标准输出流被完全缓冲。


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


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.

解决方案

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.

这篇关于简单C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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