简体   繁体   中英

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

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()'

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.

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

gcc hello.c -o hello specifies that you want to create an executable named hello.exe.

You must compile all C files and then run the executable to run the program. You cannot actually run the .c files. When you don't specify an exe, it just compiles. The fact that you have no errors is a good thing, because it means that your code is correct! :)

The first time, your executable was named "a.out" (or "a.exe" on Windows), which is the historic default name given to an executable by gcc.

You should spend some time figuring out the difference between source and executable, and between opening and executing, too...

Welcome to programming, and Stack Overflow!

gcc hello.c

compiles the file and generates an executable file 'a.out', but does not execute it. That is why nothing more happened on your command line.
To start execution, you would have to type:

./a.out

just as you did for 'hello' if i understand correctly.
You will have to learn the difference between compilation, linking and execution if you want to program in C, find yourself a good beginner's book or tutorial, it's worth it!
Cheers!

You got the

syntax error near unexpected token `('
`int main()'

error when you set the protections on your hello.c source file and tried to run it.

Using gcc transforms your .c source file into a binary program you can run.

gcc -o hello hello.c

The order I've used is not critical. I am just used to compiling this way. You compiled this way

gcc hello.c -o hello

which is fine.

The successful compilation and link created an output file called hello, which is what you would run

./hello

Have you tried including #include<conio.h> and include getch() before return 0; to display the terminal window.

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