C Programming on Raspberry Pi.
After seeing the heading, you might be asking why C?
And why on Raspberry Pi?
First question probably is easy to answer and it has a lot of answers. Why C?
Because it might be mandatory in your syllabus.
Maybe you want to program for Arduino.
Probably small mathematical problems need to be solved.
Maybe you want to get a hang of programming and C is an awesome place to start.
So, lots of good reason for learning C.
But why on Raspberry Pi?
Here things are not that straightforward because if you have a computer even a cheap and old one will perfectly compile C codes. To learn C that computer is more than enough. Why bother with Raspberry Pi then?
Well let’s look at some scenarios, how about your computer is out of action because of a bad drive or something? Maybe Raspberry Pi can help.
How about getting distracted a lot when you want to actually gets thing done? Maybe a Raspberry Pi can help, it is not that powerful so you can’t just jump in YouTube and watch some videos and by some, I mean a lot.
What if you don’t have a computer? Well then, a Raspberry Pi should definitely help you out.
For me these are good enough reason to play with C in Raspberry Pi. I will just keep my options open should I ever need it.
Let’s look at what do we need for that
1. Raspberry Pi 2/3, Micro SD card, Power Supply, Mouse, Keyboard, Monitor.
2. Internet Connection (optional, as I think Raspbian comes with an IDE, might be wrong though)
3. Fully updated Raspbian Operating System.
To write down a code and compile it we need a comfortable place i.e. an IDE. It will provide us with GUI, Code Formatting etc. Heavy weight IDE that runs on PC might not run well on Raspberry Pi due to its limitation. There is however a good light weight IDE that I have been using for a long time now and serves the purpose pretty well. It is called Geany.
If it is not already installed, we need to run this simple command to install it.
sudo apt-get install geany
After installation Geany can be launched from Application Menu > Programming > Geany.
After successful launch we will be greeted with this simple GUI.
From here on we can write code and compile them. So, let’s go with the tradition and Write down a Hello World code and Compile it.
To do so we have to go to File Menu > New (With Template) > Main.c. Which will bring up an empty C file.
We can see the main function is already there so we will just add the hello world part. Don’t worry about the text in red, those are commented and won’t impact the code.
Now once we write the code, we have to save it. Do keep in mind to keep the file extension and save it in a convenient location because I will show another way of compiling the code shortly.
After saving the Code we have to Build it. If we have any mistake in the code we will be notified here.
As there was no mistake Compilation was a success. After that we can click the “Run or view current file” button to actually see the code work.
And here it is, Hello World with all its glory.
Like I was saying there is another way of showing it that is from the compiler. To do so Open up terminal and go to the location where the file is saved.
To do so you can simply cd space the location and hit enter.
After that you can list files to verify if you are in correct folder just by typing ls.
Now to show the file we will simply type
./helloworld and hit enter.
And you can see Hello World is being printed. Two things to note here
1. In the first attempt I mistakenly typed helloworld with a capital H and didn’t work. You have to make sure to use proper case for this.
2. Another interesting thing here is see how just after the Hello World in the same line we have other things? It is because we didn’t end the Hello World with any kind of “New Line”. Had we put that; we would have gotten just Hello World in that line.
Let’s not end here. Let’s do one more code. In here we will take two numbers and find the average value of them.
Though it is not a C Programming Tutorial but still let’s go through what is happening.
We first declare 3 Variables a, b, c. All of them are integer type.
Next, we use scanf to get those two values from us. We store them in variable a and b.
Then we add those two values and divide them by 2. That is the formula for finding average. We also store that data in another variable, c.
Finally, we use printf to output that variable c to terminal.
In there we put in 25 and 15 and average is 20. Which is correct.
If you are new to C, I bet this was fun.
I will be honest here, programming on Raspberry Pi is a novelty for me because I have a powerful computer but for some people it might be a necessity. One more thing, if you happen to teach people about C, how to program with C, you can hook it up with a Projector and teach them. How cool is that! Last but not the least it is fun.