Posted September 29, 2009 by Spyros in C/C++ Programming

Linux Programming in C Basics


If you are a windows programmer, linux programming might seem a bit daunting at first but it really isn’t. Most of you will feel pretty comfortable with, or just a bit awkward mostly for using the shell. In linux, it’s quite standard that C programs are compiled under the GNU C Compiler named GCC. As with every GNU tool, you can download this for free in your linux machine, maybe even using your package manager(synaptic for instance) or shell (apt-get install gcc) for debian-ubuntu users(like me :D).

If you know C Programming, or at least the basics of it, you will easily understand the following snippet of code. Let’s see it together and i will explain more below:

#include <stdio.h>

printf("C Programming For Linux - My first program ! ");

This is the standard way to print something at a c program. We just include the file stdio.h which is included so that we can use the printf command. We open the main function, which is the entry point of our program and we just print the string “C Programming For Linux – My first program !” also appending for a new line after the string. Main() returns void, which means that it returns nothing and it receives no arguments.

Let’s save this code with the filename example.c. In order to compile this little program, we use the command:

gcc -o example example.c

We call gcc with the -o switch which means something like -output and finally we pass the source filename. So to remember which one is first, the binary filename or the source code filename, just remember that -o stands for -output, the executable.

Of course, in order to do that you need to have opened the shell of your linux distribution. If you don’t know much about the shell, i’ll be writing an article soon to explain more about it. For now, you just need to now the cd command, the command that is used to change directory. So, if you created the source file at your desktop, you can open your shell and write “cd Desktop” and then execute the command above.

After you execute the command you now have an executable file named example. You can execute it by giving the command:


And it now prints “C Programming For Linux – My first program !” at your command line.

These are the basics for creating an easy linux c program. In the next tutorial i’ll be showing how to create a more complex program with more source and also header files.