Creating Your First C Program on the Raspberry Pi

«« Previous
Next »»

Writing Your First C Program on The Raspberry Pi


LPI Certification, LPI Tutorials and Materials, LPI Guides

We offer training and blogs in many languages. Some of these languages are scripted and others compiled.  With a compiled language the source code is transformed into a binary program. When using a scripted language the code is translated at run time by the script interpreter. In general compiled programs should be quicker to run than a scripts when running the same task  In writing your first C program you will learn to use a compiled language. This is where you are creating your own binary or executable programs.

Whilst C is not going to be the easiest of languages to learn it is a great way to start to understand computers and programming. In the Linux community we talk a lot about the value of Open Source programs and when you write you own program you will discover what we mean by Open Source. Many programs in Linux and other Operating systems are written in C making a good choice to learn.

The Learning Environment


We can use Graphical tools and even IDE or Integrated Design Environments to create the code. To keep this tutorial as simple as possible and reducing the need for reliance on additional programs  we will use command line text editors . In this tutorial we will start with nano to get you into the idea of creating the code. In later tutorials I will use vim. I would always recommend vim but if you have not used it before it takes a little getting used to. We want to focus and creating our first C program and not the editor.

Throughout this series of tutorials we will use a Raspberry Pi computer running the Raspbian operating system. It is not a problem using any Linux distribution on the hardware of your choice. It will be sufficient to follow through. Writing in C here is the focus and we do not mind which platform that you choose.

Logging onto the Raspberry Pi we are presented with the GUI desktop or perhaps the command line if we have not enabled the desktop. We are using the desktop but it again does not matter as we will be making use of the command line. Using the desktop allows us to display text in a larger size for the purposes of the video. Using nano as the text editor keeps the editing simple.

Creating the Code


To start we will be creating a directory to organize our code and help us locate files we need. Making sure that we are in our home directory we use the mkdir command to create the structure we need.

cd
mkdir -p src/c

Creating the directory named “src” allows storage of our source code, additionally the sub-directory “c” allows us to organize the source by language. Keeping all files organized makes baking them up easier, and that is not a bad thing.

Moving on with our code-creation we can additionally start our editor and create our first code.

nano hello.c

Using the extension “.c” , as we have here, allows us to easily know the purpose of the file. Entering our new file we can easily start adding code and we often will expect to see comments at the start of the file. Comments lines are for the reader only and are not interpreted by the compiler, additionally, they help our understanding of the code.

Adding Comments


Ensuring we add great comments to our first C program we will take a look at the two styles of comments available.

◉ Single line or in-line comments: //
◉ Block or multi line comments: /* */

Looking at the first lines of code now we can see the use of both comments types:

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/

Adding comments to your code is always appreciated when the code is read by others, additionally you may appreciate it years to come when you revisit the code.

Adding Libraries


The next line that we are adding is a pre-processor command. This tells the compiler to include additional code libraries during the compilation. This libraries will contain pre-written code that we can make use of.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>

Using diamond brackets around the library name tells the compiler to locate the file, stdio.h in standard system directories rather than adding the full path. The file is actually located in /usr/include/ on the Raspbian OS.

Adding the main Function


Rapidly moving on as fast as out little fingers can type we will create the function main. Each and every C program than can be executed must have a main function. Creating main with the bare minimum we add the following code.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>
main () {

}

Using parentheses we can group together arguments that may be passed to the function and the braces contain the code. Seeing that the braces are empty we can beginning adding code to the main function.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>
main () {
  printf("Hello World\n");
}

Noting the code that we have added within the braces we use the printf function. This function is located in the stdlib file that we included earlier, additionally, we can see that pass an argument to printf. The arguments are grouped within the parentheses and we have one quoted string. Using printf we can print to the console screen. We print the text and additionally, the \n represents a new line after the text. There are other special characters that we can print such as \t representing a tab and also we can use \b representing a backspace. Adding a semi-colon to the end of each line tells the compiler that it it the line end.

Congratulating yourself with a big pat on the back this is your first C program, or at least, the source.

Compiling the Program


Having spent hours slaving over the keyboard to create the source code we are ready to compile the code. As a result we will have an executable program. Using Linux we have have gcc, the Gnu C Compiler. Raspbian has this installed by default, while other distributions may need this installing. Using the compiler is easy as we will see in the next code snippet.

gcc -o hello hello.c

Using the option -o tells the compiler the output file to create. Although we have created the binary file now we still have the source code located in the hello.c file. Making the source code available with the binary is creating Open Source Software. As a result you are  contributing to the Linux community.

Executing the Program


Having now created the program we can execute it and see the results of our labors

./hello

Extending the Program


Having created our first C program on the Raspberry Pi we can move onto are next release of the software. Rather than making do with static text we can make the program more dynamic by passing arguments.

//This is an inline comment, commenting the rest of the line
/* This is a block comment that
continues across many lines
until it is closed*/
#include <stdio.h>
main (int argc, char **argv) {
  printf("Program %s is running\n", argv[0]);
  printf("Hello %s\n", argv[1]);
}

Locating the colored test you see the additions that we have made, additionally we detail these for you. Locating the main function you will notice that it now accepts arguments and we define the number of arguments with argc. This is an int or integer datatype. The next argument listed is a character array, argv. This will contain the arguments that we pass though to the program. Adding code to printf we add string place holder with %s. These placeholder are populated in the following arguments. Using argv[0] we print the program name and argv[1] represents the first arguments passed to the program at run time.

◉ argv[0] : Index 0 is the first element in the array argv and represents the program name
◉ argv[1] : Index 1 is the second element in the array and represent the first argument passed to the program

Running the program with an argument will now effect the displayed message. Don’t forget to compile again.

gcc -o hello hello.c
./hello fred

The video will step you through the process.



«« Previous
Next »»

0 comments:

Post a Comment