Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

Linux and C – “Hello, world!”

Since the release of K&R, every programmer has become familiar with the “Hello, world!” program. I find that learning to build this program from start to finish is an excellent introduction to the tools available in Linux.

Setup

First, we want to create a directory for our programs. I recommend ~/Documents/Programming, because this is a simple solution. In order to do this, we need to navigate to the Documents directory using cd, create a new directory using mkdir, and navigate into the new directory using cd.

To do this, open a terminal and type the following:

cd ~/Documents

mkdir Programming

cd Programming

Now we are ready to begin programming, so open the new file in vi using the following command:

vi helloworld.c

The Code

Copy the following code into your new file. You can either retype it, or highlight the text, copy it, and use the right-click menu to paste it into vi.

REMEMBER: you must put vi in INSERT mode before typing in the code.

[code language=”c”]

/**
helloworld.c
This program is created independently by Robert Beisert
This program is based on the original “Hello, world!” program from K&R __The_C_Programming_Language__

Prints “Hello, world!” to the terminal
**/

//In order to use functions from other sources, we have to use #include
//printf() comes from the file stdio.h
//stdio.h is a default library, so we surround the file name with <>
#include <stdio.h>

/*
This is a comment block
C comment blocks are defined by the symbols you see here
*/

/*    Comment blocks can be on one line, if you want */

//Inline comments, defined by the two slashes, make everything else on the line into a comment

int main(int argc, char *argv[])
{

//Prints text directly to the terminal
//”\n” is the newline character – it acts like the ENTER key in Microsoft Word.
printf(“Hello, world!\n”);
//This function is designed to return an int
//In order to make these funtions work properly, we have to return some integer
//Conventionally, we return 0 if everything worked properly
return 0;

}

[/code]

Compiling

We will now use gcc to compile our program. There are a few basic gcc options you really need to know, though we won’t be using all of them this time:

-g      Compile the program with symbols, so you can debug it with gdb

-l       Link to a non-standard library in the /usr/lib directory (like SSL, etc.)

-o      Name the program. Otherwise, gcc will name it “a.out”

-Wall      Warn us about everything the compiler can think of

In this case, we don’t need to debug the program, and we won’t be using non-standard libraries. We won’t even be using variables, so –Wall is irrelevant. We do want to name the program, however, so we should use the -o option.

In the terminal, type the following:

gcc -o helloworld helloworld.c

Assuming you copied the code correctly, the compiler should quietly work quietly for an instant. If you check the directory, you should now see an executable file named “helloworld”.

To list the contents of your current directory, you can use the terminal command ls. By default, all executable files should be colored differently from other files.

We now just have to run our program in the terminal.

./helloworld

We have to start the command with “./” because the program is in the current directory (.). Otherwise, it will check the /bin directory for a program named helloworld, which it probably won’t find.