Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get no output for very simple Hello World program?

Tags:

c

I am wondering why my C program does not print any information. I am 100% new to programming, and have been able to learn a few things in Ruby and Python these past few weeks, but I am getting nowhere with the C stuff. Here is the total baseline, simple program that everyone learns first:

#include <stdio.h>

int main()

    {
        printf("Hello World\n");
        return 0;
    }

So I've got that written, and I save as hello.c. Now I cd to the directory where it is, and then I try

gcc hello.c

and nothing happens - no errors, just nothing. If I write instead

gcc hello.c -o hello

a new executable file is written to my directory, and when I open it, it looks like the normal command line, and has "Hello World" written there, as I expected in my terminal the first time.

I also tried make hello.c an executable itself, but when I do that I get

syntax error near unexpected token `('
`int main()'
like image 531
user1451632 Avatar asked Jun 12 '12 15:06

user1451632


People also ask

Why is my C program not showing output?

Solution 1. The problem is that when you run a console app, it expects input from the user - and if it doesn't get it, it doesn't wait around - it closes and the console window is closed as well. As a result, you can't see anything on the screen because it just isn't there for long enough.

Why is Turbo C++ not showing output?

First make sure your code compiles successfully! And to see the output screen in Turbo C++ press Alt + F5. What ide should be used for C++ coding for beginners?

What is the command for Hello World?

Open the command prompt and cd to the directory that contains the . java file. Type javac HelloWorld. java and press enter.


2 Answers

When you type

gcc hello.c

and nothing happens, that is to be expected. Or more precisely, GCC will compile an executable with the default name, which for me is a.out. If I were then to type ./a.out on the command line, I see the output.

I think there may be a slightly bigger conceptual issue here though from your Ruby/Python background. Ruby and Python are (usually) interpreted languages, which means that when you create a script file, you can mark it as executable and through some magic the OS will start up a program that reads the files and executes them for you. C, however, is compiled. So when you run GCC, that takes your source file and turns it into an executable, with either the default or specified name. At this point, one would not expect to see any output unless there is a problem with the compilation process. Then you can run ./hello, which is an executable, and see your output.

That also explains why you can't mark hello.c as executable and run it. It needs to be compiled into an executable first. It looks like the system is pretending it's a shell script, which is isn't, and giving you a syntax error.

like image 142
dsolimano Avatar answered Oct 29 '22 08:10

dsolimano


gcc hello.c will generate a file named a.out
gcc hello.c -o hello will generate a file named hello

These are executable files, and you need to execute/run these to get the output.

Run these as

./a.out or ./hello

like image 42
phoxis Avatar answered Oct 29 '22 08:10

phoxis