Using the GCC C compiler

The Pi comes with the GCC C compiler already built in. However, compiling a program on the Pi is different to Windows because Linux has no concept of file extensions nor what to do with files that might have them. It's good practice, for your own benefit, to use file extensions so that you know what each type of file is for. So, create your C program using your preferred editor:

nano hello.c

Type in a simple program:

#include <stdio.h>

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

{

    printf("Hello world!\n");

    return;

}

Save the changes and compile:

gcc hello.c -o hello

hello.c is your source file. The -o hello specifies that the executable output file should be called hello. hello is just a file and not something that the Pi can run immediately. Linux requires that executable files have a particular set of file mode bits to specify that the file is executable. You use the chmod command to change the bits. The +x adds the executable bit to the file's permissions:

chmod +x hello

You can now run the program but cannot just type its name, rather, you need to prefix the filename with ./. You need to do this because your current directory is probably not going to be in the PATH variable. The PATH variable specifies the list of directories to be searched when looking for a program to run. If you modify your PATH variable to include your current directory, or copy the program executable to a directory that is already in PATH, you can omit the ./.

./hello