In this tutorial, I’ll discuss what the C programming language is, what C programming is used for, and how to write and run a C program on the Raspberry Pi.

The intent of this article is to give you a very basic introduction to C programming on the Raspberry Pi. If you’re looking for more in-depth information on C programming, a great book to have is The C Programming Language by Brian Kernighan and Dennis Ritchie. It’s a useful reference for both experienced programmers and anyone that wants to learn the C language.

What is a C Program?

The C programming language is one of the most widely used programming languages of all time. Programs written in C can be run on a wide range of platforms including personal computers, embedded microcontrollers, and supercomputers.

One advantage of C is that the code runs almost as fast as assembly code. Like assembly code, C lets you access powerful low level machine functions, and it has a syntax that is easier to read than assembly code. For example, compare this assembly code for a “hello world” program to the C code for the “hello world” program below:

        .arch armv6
        .eabi_attribute 27, 3
        .eabi_attribute 28, 1
        .fpu vfp
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 2
        .eabi_attribute 30, 6
        .eabi_attribute 18, 4
        .file   "hello-world-assembly.c"
        .section        .rodata
        .align  2
.LC0:
        .ascii  "Hello, World! \000"
        .text
        .align  2
        .global main
        .type   main, %function
main:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {fp, lr}
        add     fp, sp, #4
        ldr     r0, .L2
        bl      puts
        mov     r3, #0
        mov     r0, r3
        ldmfd   sp!, {fp, pc}
.L3:
        .align  2
.L2:
        .word   .LC0
        .size   main, .-main
        .ident  "GCC: (Debian 4.6.3-14+rpi1) 4.6.3"
        .section        .note.GNU-stack,"",%progbits

 

High level programming languages (like Python) provide programmers with commands that make it easy to do tasks like printing text to the computer monitor and logic functions like and, or, and not. Low level programming languages like assembly only give you access to the machine’s basic instruction set. The C language is a mid level programming language, which has the benefit of providing useful and easy to use functions, while at the same time is powerful enough to let you control a computer’s basic operations.

BONUS: Download the Raspberry Pi programming cheat sheet – a one page PDF guide with instructions on how to create and execute C programs, Python programs, and Shell scripts.

What Can a C Program Do?

C was initially used to develop operating systems, so it might not surprise you that the Linux kernel is written in C. C can do pretty much anything you would want to do in computer programming. Some example applications include:

  • Operating systems
  • Large programs
  • Databases
  • Desktop utilities
  • Language compilers
  • Text/photo editors
  • Network drivers

How to Write and Run a Program in C

To demonstrate how to create a C program, compile it, and run it on the Raspberry Pi, we’ll make a simple program that will print “hello world” in the terminal.

The coding process in C consists of four steps:

  1. Creating the source file
  2. Compiling the program
  3. Making the program executable
  4. Executing the program

Creating the Source File

To start, open the Nano text editor and create a new file with a “.c” extension by entering this at the command prompt:

sudo nano hello-world.c

This file is where you’ll write the C code. You can write the code in any text editor, just make sure to give the file a “.c” extension.

Now, enter this code into Nano:

#include <stdio.h>

int main()
{
   printf("Hello, World! \n");
   return 0;
}

 

After entering the code, enter Ctrl-X and Y to save and exit Nano.

Compiling the Program

Code written in C will need to be compiled before it can be run on a computer. Compiling is the process of converting the code you write into machine readable instructions that can be understood by the computer’s processor.

When you compile your source file, a new compiled file gets created. For example, entering the command below will compile hello-world.c into a new file called myfirstcprogram:

gcc hello-world.c -o myfirstcprogram

Making the Program Executable

Now we need to make the compiled file executable. To do that, we just need to change the file permissions. Enter this at the command prompt:

chmod +x myfirstcprogram

Executing the Program

Now all we need to do to run the compiled, executable, C program is enter this at the command prompt:

./myfirstcprogram

Hope this helps you get a basic idea on how to get started programming in C on the Raspberry Pi. If you have any questions, please leave a comment below, and if you know anyone who could enjoy this information, please share it! You can also get our tutorials in your email by subscribing!


Need an easy-to-use way to design circuits and layout PCBs?

Try EasyEDA, a free circuit design software that also offers low cost, high quality PCB manufacturing.