C language is one of the earliest programming languages. It is simple and easy to learn. Formerly C programming was performed on Turbo C, a discontinued integrated development environment. But nowadays it can be easily executed on different operating systems.
In this guide, you will learn how to write your first C program using the Linux operating system which requires just the GNU C compiler and a text editor and not a full blown integrated development environment to get started. The following steps will show to install a GNU C compiler on Linux, how to write the source code, compile and execute the C program.
Requirements
To execute a C program on Linux, you need to install the following :
- GNU C and C++ compiler collection
- Development tools
- Development libraries
- IDE or text editor to write programs
Step 1: Installation of the compiler on Linux
For C programming it is essential to install a compiler that will compile the program before executing. In Linux operating system type the following command in terminal to install the GNU C compiler which is one of the most famous compilers for Linux:
$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev
Output:
Step 2: Verification of installation
To ensure that your compiler is installed type the given command to find its location and version number:
$ whereis gcc
Output:
$ which gcc
Output:
$ gcc --version
Output:
Step 3: Open text editor on Linux
To start writing your source code, open a text editor by typing
$ gedit program.c
Step 4: Write your first C program
Write the C source code given below in text editor:
#include
main()
{
printf("This is my first C program on ubuntu\n");
}
Close the editor window.
Output:
Step 5: Compile your program using GNU C compiler
The following command will invoke the GNU C compiler to compile the file program.c and output (-o) the result to an executable called program.
$ gcc -o program program.c
Output:
Step 6: Execute your program
Type the given command to display output:
$ ./program
Output:
Wrapping Up
The above guide demonstrates how to write your first C program in Linux by installing GNU C compiler. To know more about C programming in Linux explore different books and practice more source code related to different topics in C programming.