Skip to main content
LPI Linux EssentialsRaspberry Pi

Raspberry Pi and your first steps into C programming

By November 29, 2013September 12th, 2022No Comments

LinuxEssentialsBorderSMALLEvery first step has to be valued and seen in the context of a step in the right direction. Without a first step the winning line will never be crossed. Without that first step man would never have made it to Watford let alone the moon. Those first steps though can be the difficult and challenging ones as we struggle with the enormity of what lays before us. If we want to master the computer rather than be mastered by it we need to take are first steps into programming and we can use C to do this. C is a programming language that has been about since the existence of Linux itself. By the end of the tutorials we will be able to say Hello World and customize a message for the user. If you are reading this as part of the Linux Essentials for the Raspberry Pi then for the exam you will bee expected to know what C programming is but not any code details.

Compiled rather than interpreted

Using we will have to compile our code into a binary file that we can execute. We start with a text file, usually with a .c extension so we can easily identify the type of file it is. We then use a compiler, in our case gcc(/usr/bin/gcc) and create the binary file that we can execute. This type of complied code is more efficient than interpreted code that we may create with a bash shell script or python script. These files are always in their text format and are interpreted into binary code when they are executed.  Compiled code is more efficient but has the down side of being compiled for a specific architecture and operating system rather than scripts that can be easily run on different machine architectures.

Structure a C program

A C program will normally have a method called main. Methods are something the program can do, when we execute the program then the main method will run, this may call other methods in the program. You will normally expect include statements to bring in code from other C libraries, even to print to the screen we will need to import the methods that achieve this.

#include <stdio.h>
 main () {
   printf(“Hello Worldn”);
 }

In this simple C program we first include or import the library headers for stdio. The method printf depends on this, a quick check of the man page for printf: man 3 printf , will show that we need the stdio.h for this.

After the included statement we declare the main method, in this case it takes no arguments, main (), the empty parenthesis show this. The code block that delineates the start and end of the method are marked with { and }. The open and closing of the code block. Within the code block and then part of the method main we have the printf statement that will print to the screen Hello World.

Compiling the code

We now must take the .c file and create the binary executable, the actual program. Using the gcc we manage this with:

gcc -o hello hello.c

We create the output file, the compiled binary, hello by reading in the text file, (source code), hello.c. Hopefully we will not have any compiler errors and the code will compile successfully. Congratulation, you are a programmer and we can now test the file works:

./hello

The file is executable by default so we do not need to add the execute permissions, the compiler does this.

Making the program more dynamic

This is all it does, you can run it 1000 times and it will still only display this static text. So we can add a little more at this stage so we can pass arguments though to the program. For this we need to declare the main method in such a way that it may take these arguments at run-time

main(int argc, char **argv)

We define here two arguments, the arguments count that is an integer identified by argc and an array of arguments identified by argv that can contain character data.

If we now modify the code contained within the method main as such:

#include <stdio.h>
 main ( int argc, char **argv) {
   printf("Program: %s is runningn", argv[0]);
   printf("Hello %sn", argv[1]);
 }

We now print two lines with printf. The first displays the program name, identified by argv[0] and the second line now greets the user by the using argv[1]. For this to work the program needs to be executed with at least one argument, don’t forget to recompile the code before you execute it:

./hello fred

In this case argv[0] is the name of the program , hello and argv[1] is fred.

The video can be viewed on Youtube: