This is an introduction to the Hello World program example on the Raspberry Pi written in C.
The following example makes use of the command line interface and is set up as a step-by-step guide.
Along with creating a Hello World program, the reader will be introduced to simple linux command line commands.
Note: This introduction was written for beginners.
First step:
Making a directory that will contain source code.
cd
mkdir programs
cd programs
Second step:
Writing your first program.
Linux systems offer a great variety of text editors, natively you will find Vim or Nano.
This example will make use of the Nano text editor.
nano helloworld.c
The following code is the source code for the Hello World program:
/* My first program */
#include<stdio.h>
int main()
{
printf("Hello World\n");
}
ctrl + x
to exit the editor, hit y
and then enter
to save the changes.ls
to check if the file is present in your directory.Third step:
Compiling your first program.
helloworld.c
we need to use a compiler, we will use thegcc helloworld.c -o myfirstprogram.bin
The source code file is offered as an argument to the GCC compiler and -o
defines another argument expressing that we would like the compiler to output something.
In this case we want it to output a .bin
file that we named ourselves.
There is several other arguments you can use when compiling with GCC, an example would be
-wall
which enables all warnings. This gives you information about any error GCC might encounter.
Fourth step:
Running your first program.
./
in front of the name of the program that you want to run../myfirstprogram.bin
The command should execute the program and produce Hello World
in the console window.