0

我是编程爱好者和新手,尝试这个简单的 c 语言代码并用 gnu 编译器编译它。这是我的代码:

/*simple program:trying to printf string and get a letter*/

#include "stdio.h"

int main()
{
    int i=0;
    char c;

    while(i++<100){
        if(i % 2)
            printf("%C this is even number",i);
        if(i==50){
            c=getchar();
            printf("you enter %c letter",c);
            }
    }
    return 0;
}

但是,当我用 gcc 编译它并在终端中运行时,它没有显示任何内容,没有错误也没有警告。我试图通过改变我的代码行为方式来解决这个问题,但仍然无法正常工作。是错误的代码还是 gcc 有错误,或者我可能错过了什么?

4

3 回答 3

3

printf尝试在文本末尾添加换行符:

printf("%C this is even number\n",i);

也是i如此int使用%d格式:

printf("%d this is even number\n",i);

编辑:我刚刚用更改测试了这个,它对我有用。

于 2012-06-27T15:37:51.693 回答
0

你应该fflush(stdout)在你的展示之后打电话。

于 2012-06-27T15:41:06.123 回答
0

它在 Windows Vista 下使用 cygwin 为我运行。

我将您的代码粘贴到一个名为 test.c 的文件中并运行gcc test.c该输出一个名为 a.exe 的文件,然后我运行该文件。

johnma@johnma-PC ~
$ ./a.exe
 this is even number♥ this is even number♣ this is even number this is even numb
 this is even numbern this is even number◄ this is even number this is even numb
er this is even number this is even number↓ this is even numberthis is even numb
er this is even number this is even number! this is even number# this is even nu
mber% this is even number' this is even number) this is even number+ this is eve
n number- this is even number/ this is even number1 this is even number
you enter
 letter3 this is even number5 this is even number7 this is even number9 this is
even number; this is even number= this is even number? this is even numberA this
 is even numberC this is even numberE this is even numberG this is even numberI
this is even numberK this is even numberM this is even numberO this is even numb
erQ this is even numberS this is even numberU this is even numberW this is even
numberY this is even number[ this is even number] this is even number_ this is e
ven numbera this is even numberc this is even number
johnma@johnma-PC ~
于 2012-06-27T15:42:14.090 回答