Cannot run a program in RedHat

I wrote a simple program

int main(){ printf("hello word!"); return 0; } 

I compiled it with gcc -o hello hello.c (no error), but when I run it in terminal using ./hello , I don’t see anything, why? thank you in advance

+4
source share
6 answers

There may be a missing newline, so the output is distorted with the following prompt.

Try:

 printf("hello world\n"); 

This version also uses a more conditional message.

+7
source

Add \ n to the printed line so that the output buffer gets to your terminal.

 printf("hello world!\n"); 

In addition, you must include the stdio header to avoid implicit references.

 #include <stdio.h> 
+1
source

It may display a message, but then the application terminates immediately and you cannot see it.

After the message is displayed, call the function that reads the next key press to prevent the application from terminating until you press the key.

0
source

enable stdio.h

 #include <stdio.h> 

and try to clear stdout

 fflush(stdout); 

The standard output buffer may not have been flushed. Remember also that if the C program is interrupted, it is possible that some printf was called successfully, but the buffer was not flushed, so you can’t see anything. If you want to be sure that printf was called correctly, then manually clear the output.

0
source

Missing #include <stdio.h> . You should have received a warning message, for example: warning: incompatible implicit declaration of built-in function 'printf' . Then, depending on the system, you might or may not get the desired result.

0
source

The C language specification indicates that the last line of output in a text stream may need \n at the end. The language says that this requirement is implementation-defined. This immediately means that in the general case the behavior of the program is not determined if there is no \n at the end in the output line to the text stream.

Behavior is defined only when you are talking about some specific implementation. Some implementation may result in release. Some other implementation cannot produce anything. And another implementation may behave differently.

For your specific implementation (GCC on Linux), I expect to see the result even without the trailing code \n . Maybe there is something about how your shell / terminal is configured, which makes it invisible.

0
source

All Articles