无法在RedHat中运行程序
我写了一些简单的程序
int main(){
printf("hello word!");
return 0;
}
,我使用 gcc -o hello hello.c 编译它(没有错误) 但是当我在终端中使用 ./hello
运行它时,我什么也没看到,为什么?提前致谢
I wrote some simple program
int main(){
printf("hello word!");
return 0;
}
I compiled it using gcc -o hello hello.c
(no errors)
but when I run it in terminal
using ./hello
I see nothing, why? thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能是缺少换行符,因此输出与下一个提示符相混淆。
尝试一下:
这个版本还使用了更常规的消息。
Could be the missing newline, so that the output is mangled with the next prompt.
Try:
this version also uses a more conventional message.
将 \n 添加到打印的字符串中,以便输出缓冲区刷新到您的终端中。
此外,您应该包含 stdio 标头以避免隐式引用
Add \n to your printed string so the output buffer get flushed into you terminal.
Moreover you should include stdio header to avoid implicit references
也许它确实输出一条消息,但应用程序立即终止,您无法看到它。
显示消息后,调用一个函数来读取下一个击键,以防止应用程序在您按下某个键之前终止。
Perhaps it does output a message but then the app terminates immediately and you are unable to see it.
After displaying the message, call a function that reads the next keystroke to prevent the app from terminating until you press a key.
包含 stdio.h
并尝试刷新标准输出
也许标准输出缓冲区尚未刷新。还要记住,如果 C 程序被中断,则可能某些 printf 已被成功调用,但缓冲区尚未刷新,因此您看不到任何内容。如果您想确定 printf 已被正确调用,请手动刷新输出。
include stdio.h
and try to flush the stdout
Perhaps the standard output buffer hasn't been flushed. Remember also that if a C program is interrupted, it is possible that some printf has been called successfully, but the buffer hasn't been flushed so you don't see anything. If you want to be certain that a printf has been called correctly, then manually flush the output.
缺少
#include
。您应该收到一条警告消息,例如:警告:内置函数“printf”的隐式声明不兼容
。然后,根据系统的不同,您是否可以获得您想要的输出。The
#include <stdio.h>
is missing. You should have received a warning message like:warning: incompatible implicit declaration of built-in function ‘printf’
. Then depending on the system you could get or not the output you wanted.C 语言规范规定,文本流中的最后一行输出可能需要在末尾添加
\n
。该语言声明此要求是由实现定义的。这立即意味着,在一般情况下,如果程序输出到文本流的行末尾没有\n
,则程序的行为未定义。仅当您谈论某些特定实现时,行为才会被定义。某些实现可能会产生输出。其他一些实现可能不会产生任何结果。还有一个实现可能会以其他方式运行。
对于您的特定实现(Linux 上的 GCC),即使没有尾随
\n
,我也希望看到输出。也许您的 shell/终端的设置方式有些问题导致它不可见。The specification of the C language states that the last line of output in a text stream might require the
\n
at the end. The language states that this requirement is implementation-defined. This immediately means that in general case the behavior of the program is not defined if its line of output to a text stream does not have a\n
at the end.The behavior becomes defined only when you are talking about some specific implementation. Some implementation might produce output. Some other implementation might produce nothing. And yet another implementation might behave in some other way.
For your specific implementation (GCC on Linux) I'd expect to see the output even without the trailing
\n
. Maybe there's something about the way your shell/terminal is set up that makes it invisible.