Beginning raspberry pi c programming tutorial
The C programming language is a middle level language because it contains both features of high and low level features, although this is still a matter of debate on the internet. The C language was development by Dennis Richie in 1972 while working at Bell Labs. It was developed as a language to write operating systems so you can imagine what the C language is capable of.

You will write a simple C programming that will display the words, hello world program. You will use gcc compiler to compiler the program. The gcc program will also point out any errors that will occur during the compiling stage.

First determine if the gcc compiler is installed on your Raspberry Pi.

gcc – v

The final line will display the version number.

..

gcc version 4.4.5 (Debian 4.4.5-8)

If you receive an error because the gcc program isn’t installed you can install it by entering the following command.

$ sudo apt-get install gcc

To create a C program you will need a text editor to create your source code. Nano will be the preferred choice. Create a directory to store your C programs in.

$ mkdir csource

$ cd csource

Create file called hello.c inside the csource directory.

$ sudo nano hello.c

Enter the following code into the Nano editor.

//this program will display hello world

#include <stdio.h>

int main() {
printf(“Hello World”);
return 0;
}

 

Save the file and exit Nano. You have created the source code but now you need to take this code and create an executable program from it. This is done by entering the following.


$gcc -o hello hello.c

You are asking gcc to create the executable file hello, from the hello.c source file. If no errors occur, list the files.

 

$ ls -al

..
-rwxr-xr-x 1 pi pi 5164 Feb 18 13:02 hello
-rw-rw—- 1 pi users 82 Feb 18 13:02 hello.c

..

The new hello filehas been created and this is the executable program. Enter the following to execute the program.

$ ./hello

To fully understand what is going, we will trace each line explaining what is happening.

//this program will display hello world

This line starts with two // forward slashes. This means that you are creating a comment and anything that appears on this line is to be ignored by the gcc compiler. This section should include something about what the program does and the some information about the author.

#include <stdio.h>

This line starts with a # symbol which informs the compiler to include the file which appears between the <> symbols. This is known as a header file and inside this file contains all the information to display text on the screen. Most of the hard work has been done for you because inside this file contains pre-written pieces of code. For example in the code above we use a printf function to display a message. This function exists inside the stdio.h file.

int main() {

This line starts with the int keyword. This means that the function that follows main() will return an integer value. This can be used as a signal to determine if a program has succeeded or failed. If something went wrong during the execution of the program, you could return the value of -1. If everything went well, we could return 0.

What about the name of main()?

When you compile a program, the gcc compiler will always look for the main function as an entry point into the code. After the main() function appears a { bracket. This is used to enclose a block of code and in this case the block of code for the main() function.

The line below this is indented to the right. This helps with code readability which enables you to see clearly the code within the { bracket.

printf(“Hello World”);

Use the [TAB] key to indent lines. The printf is a function which will output text strings that appear between the double quotes “”. All functions have an open and close bracket (). For example a function can look like this.

printHello(). This function does not accept a text string and therefore does not accept a string in between the () brackets. The printf function however indicates that this function accepts a text string.

Finally the line ends with a ; This is much like a full stop in a sentence. It informs the compiler that we are finished with this line and it should move to the next.

return 0;

The return statement returns the number 0. At the beginning of main we used the int keyword which means that main MUST return an integer value. Here we are fore filling this commitment by returning 0 and signifying that we are exiting the main function.

}

Finally the closing curly bracket is letting the compiler know that we are finished writing code inside the main function.

Beginning raspberry pi c++ programming course

Next Raspberry Pi Beginning C++ Programming Week 7 – Day 4 >>