Reputation: 307
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
Upvotes: 4
Views: 528
Reputation: 320719
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.
Upvotes: 0
Reputation: 783
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.
Upvotes: 0
Reputation: 5539
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>
Upvotes: 1
Reputation: 39194
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.
Upvotes: 0
Reputation: 67345
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.
Upvotes: 0
Reputation: 400069
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.
Upvotes: 7