Exercise 1: writing and compiling a C program

You should do all this exercise as a normal (non-root) user. You are going to type in, compile and run a 'C' program from scratch.

Create the source file

Using your favourite editor (vi, ee, joe or whatever), create a file called hello.c containing the following text:


#include <stdio.h>
int main(void)
{
    printf("Hello, world!\n");
    return 0;
}

This is not a programming course, so you don't need to understand everything which is there. But in summary:

Compile and run

Once you have saved this file, you need to compile it like this:

$ gcc -Wall -o hello hello.c

If you get a shell prompt back, it was successful. If you get an error message, it will include a line number; the mistake is probably somewhere around that line. Go back into the editor and correct the program. Extra spaces are not important, but a missing bracket or semicolon will make the file invalid.

If everything was OK, then you can run the program:

$ ./hello
Hello, world!
$

Ask an instructor if you see anything else.

The reason you need to put ./ in front of the program name, is that the shell only looks for programs in the PATH (and your home directory is not in the PATH). Hence you need to tell the shell explicitly to look for the file in the current directory, which is "."

Extras (if time available)