Linux is a platform that can be used for the development of programs and applications using languages such as C. In fact, it is likely the best operating system for beginners due to its simplicity. We would strongly recommend that if you are just starting out programming, then you should embrace the Linux platform because it is a great world to be in.
C is a very tight and economical language, and in the current climate of open source software, it is now generally essential for software professionals to be conversant with C and how it is used with the Linux open-source operating system. Let’s have a look at what’s involved when you use C on Linux.
Here are some of the basics that will be useful to anybody using C for the first time using Linux, including installation, compiling your first program in C and then running it on the Linux OS:
Contents
How to Write Your First C program on Linux
Here are the steps involved in writing and compiling a C Program using the Linux open-source operating system. When writing code for Linux, it is just the same as for Windows/DOS if you use the ANSI C code. For example, you will not be able to use library functions that do not conform to the ANSI standard, such as those offered by conio.h and graphics.h.
You will be using the GCC compiler with Linux (Gnu C Compiler), it is part of the GNU Compiler Collection. First, you must make sure that the GCC compiler is installed on your computer. Here’s how to find out:
and write the command:
$ gcc gcc: no input files
If the GCC compiler is installed you should see:
If you get “Command Not Found” or similar, then you have to install the open source GCC package using the package manager. This package, like all open source software packages, is freely available online.
In addition to the compiler, you will also require the C standard library known as glibc so that your C files are compiled correctly:
Type in:
$ locate glibc
then check the output:
If it shows direc
'/usr/share/man/man7/glibc.7.gz' or 'foo/bar/glibc'
or the like, then you have glibc installed. If not, then you need to install it.
What this shows is that you have a text editor present, a compiler and the standard library. You are now ready to write your first line of C code on Linux!
Example: A Simple “Hello World” Message
To make this easily understood, we will now show you how to write a simple message: Hello World.
First, fire up gedit as shown in the screenshot below, although you can also use another editor such as vim.
Or you can use the following commands in terminal to open your favorite text editor, type in
For the gedit editor:
$ gedit PROGRAM_NAME.c e.g $ gedit Hello_World.c
OR
For the vim editor:
$ vim PROGRAM_NAME.c e.g $ vim Hello_World.c
The program name in our example being ‘Hello-World.c’. Now input this simple C code to print Hello World!
#include <stdio.h> int main() { printf("Hello World!\n"); return 0; }
Save this code with the name Hello_world.c. Now, compile the code using the following command:
$ gcc Hello_World.c
After executing the command, type in:
ls -l
You will see an ‘a.out’ file. This is the executable file of your C program, compiled and linked with the appropriate libraries. To execute it, run (note the leading ./, which is essential!):
$ ./a.out Hello World!
Congratulations, you have just written your first C program on Linux! That was just the normal C that you would write using DOS or Windows – no surprises there!
The Linux a.out File
The Linux a.out file is the Linux equivalent of the .exe file extension you are used to seeing on DOS – including Windows which runs above DOS. Most people do not realize that Windows is a DOS-based application.
The a.out extension cannot be executed by Windows since it is not a DOS command. It is a Linux command! Rather than having to rename your file each time you compile, you can specify the output file name to the compiler thus:
$ gcc -o Hello_World Hello_World.c
The C Programming Language Recommended Books
‘The C Programming Language’ is well-known programming book by Brian Kernighan and Dennis Ritchie, which teaches you C programming with a strong Linux flavor.
If you want to master the use of C language, then it would be a good idea to try the examples and exercise programs provided in this book.
Also, check out “Exploring C” by Yashavant Kanetkar if you want more practice in using C programming language.
[irp posts=”937″ name=”Programming on Linux Part 2: C++, Java, .Net Programming on Linux”]C Program Execution:
The diagram above shows a C executable file relies upon the underlying process for its execution. The executable is therefore dependent on the processor. This means that should any changes occur to the processor or to the Operating System, then the C source file would have to be recompiled to generate an executable compatible with the modified platform.
If you still have any questions, concerns or suggestions on this topic, please share them on our comment form below!