Networking | Programming | Security | Linux | Computer Science | About

How to program in C on Linux

C is a multi-purpose programming language. It was created on Unix systems in the 1970’s and has been used to write all of the common operating systems in use today. By learning the C language, you will learn about memory allocation, code optimisation and the underlying principles of computer architecture. C isn’t easy, it can be challenging at times, but its a great language to learn especially on a Linux machine.


The purpose of this tutorial is to:

Why learn C?

C is one of the oldest programming languages in use today and still ranks in the top ten of sought after programming languages by employers. The Linux kernel was written in C as was most of Windows and Apple OSX. C has features of both high level and low level languages and can be used to write applications for device drivers, games and web servers and pretty much anything you want. It has also been used as the foundation for many other popular languages such as C#, C++, Java and PHP.

Setting up the C environment

Setting up your environment will take one line of code. Its as simple as that.

For Debian systems you run this command from the command line:

sudo apt−get install build−essential

Here’s how you would install the same setup on an RPM-based Linux like Fedora, RedHat, or CentOS:

sudo yum groupinstall development−tools

If you have a different variant of Linux, simply search for “c development tools” and your brand of Linux to find out what’s required.

Once you have that installed, you should be able to type:

cc --version

to see what compiler was installed. You’re now ready to chose your code editor or IDE.

Choosing a code editor for C development

At this stage, don’t spend too long looking for a code editor to use for C programming. I would recommend using whatever editor you feel comfortable with. This could be geany, gedit or VScode if you prefer a GUI. If you prefer the console, you could use nano, emacs or vim.

I personally wouldn’t recommend using an IDE for C programming. They are helpful when you need to get things done, but their help tends also to prevent you from really learning the language.

In my experience, the stronger programmers don’t use an IDE and also have no problem producing code at the same speed as IDE users. People have been writing large pieces of software in C since the 1970’s with nothing more than a simple text editor at the command line.

Write your first C program

In your code editor of choice, type in the following code:

 #include <stdio.h>
 / This is a comment. /
 int main(int argc, char argv[])
    int distance = 100;
    // this is also a comment
     printf("You are %d miles away.\n", distance);
     return 0;

then save the files as test1.c

Here’s an explanation of what the different lines mean:

#include <stdio.h> imports the contents of the stdio.h header file into the program. This has a list of functions in it that you can use in your program.

int main(int argc, char argv[]) How C programs work is that the operating system loads your program, and then it runs the function named main. For the function to be totally complete it needs to return an int and take two parameters: an int for the argument count and an array of char * strings for the arguments. Each function’s content is contained within {}

int distance = 100; sets the integer variable distance = 100

printf(“You are %d miles away.\n”, distance); this print statement replaces %d in the sentence with the value of the variable distance (100 in this case)

return 0; this returns from the main function and passes gives the operating system your exit value of 0.

Compile the program

To compile the program, type in:

make test1

This will give you the following output:

cc -Wall -g    test1.c  -o test1

The make program has passed the command line arguments -Wall -g to the c compiler which then compiles the test1.c file and outputs an executable file named  test1. This can be modified by adding a Makefile in your working folder to add extra parameters and instructions.

To execute your code, simply type in:


which will output:

You are 100 miles away.

Further resources

So there you have it. You’ve created a simple C program (slightly more complicated than Hello World. From here you can check out the following resources to take further steps on your C programming journey:

C language book

The most important book to learn the C programming language from is… the C programming language by the developers of C, Brian Kernighan and Dennis Ritchie. This book has been widely reputed to be the C bible for the last 40 years or so.

C language websites

Other ways to learn C involve reading through existing code. A good resource to do this is Github, especially Linus Torvald’s Linux repository.

Its wild that you can read through the source code that runs the operating system that you’re using!

Published on Fri 13 February 2004 by Gary Hall in Programming with tag(s): programming c linux