Hello, World on C/C++ on Linux

So you came home from your first 101 computer science class and you have homework to do – write a program on C.
Where do you start? I’ll try to answer that.

The easiest way would be to use Linux. It’s already setup for you, no need to setup anything.
How to check? Open the Terminal and type gcc, and click Enter. If you see error “no input files” you are all set, if you see error “command not found” you will need install C compiler (sudo apt-get install gcc; but I never saw a system where it wasn’t present).

Next step is to type your program. There are two school of thoughts: one says you should start with command line and the second thinks command line is too obsolete and you should work in some kind of studio from the start. Personally in my professional career I never wrote an application without a studio. But I do believe that one should be able to do everything manually with a command line. So we’ll start with a command line.

If you are on Linux you should know Vi, period. It’s hard so start with but it’s a necessity – you would find Vi on every Unix/Linux system. Vi is bare-bone, I would advice to use Vim (sudo apt-get install vim), it has syntax highlighting.
So open Vim and type your program. For hello world it could be something like this:

#include <stdio.h>

int main()
    printf("Hello, World from C\n");
    return 0;

Save and close Vim (use ZZ – it’s faster). Let’s say you saved you file as hello-world-c.c.
now execute
gcc -g -Wall -std=c99 hello-world-c.c -o hello-world-c
if everything went fine you would not see anything in the terminal, but new file should appear: hello-world-c.
Execute it:
You should be greeted with “Hello, World from C”! Success!

If you see some error, address them, and try to compile again.

Now what we did here. Gcc is C/C++ compiler. And the flags we use:
-g – add debug information (so we can debug this application)
-Wall – show all errors and warnings
-std=c99 – which standard of C to use (I’ve heard that some professors require to use old style -std=c89, check with your class)
-o – name of output file (your application)

This would conclude part one. Part II would be C++ application.

Leave a Reply