简体   繁体   中英

can't run program in RedHat

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

Could be the missing newline, so that the output is mangled with the next prompt.

Try:

printf("hello world\n");

this version also uses a more conventional message.

Add \n to your printed string so the output buffer get flushed into you terminal.

printf("hello world!\n");

Moreover you should include stdio header to avoid implicit references

#include <stdio.h>

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.

include stdio.h

#include <stdio.h>

and try to flush the stdout

fflush(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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM