In this tutorial we are going to study how to write, compile and run c program in Linux. Linux
Back to content
A c program is written in a text editor and is saved with ".c" extension. ".c" helps Unix or Linux OS(operating system) to identify that it is a C program.
I am assuming that all of you can have an access to a text editor and you know how to write in it and save it.
Lets write our first c program :
#include<stdio.h>
int main()
{
printf("Yepeeeeeee this is my first C program\n"); // Displays the string as output
return 0; // tell the OS that the program exited without errors
}
Type the above code in any editor and save it with
".c" extension say
"firstprogram.c"
Explanation :
The execution of a C program starts with main() function.
// ( Double slash ) is a single line comment and is ignored by the compiler. It is used by the programmer to explain the working of the programs. so that other programmer who reads the program written by him would understand what the program is exactly doing.
/*
Written anything
.......
*/
The above syntax is for multiline comment i.e any text written in between is ignored by the compiler.
Now what does first line #include<stdio.h> mean :
The first line may be confusing, but it’s just a C syntax that tells the compiler to include headers for a standard input/output (I/O) library named "stdio.h". This header file is added to the program when it is compiled. It is located at /usr/include/stdio.h and it defines several constants and function prototypes for corresponding functions in the standard I/O library. Since the main() function uses the printf() function from the standard I/O library, a function prototype is needed for printf() before it can be used. This function prototype (along with many others) is included in the "stdio.h" header file. A lot of the power of C comes from its extensibility and libraries.
In printf("Yepeeeeeee this is my first C program\n") we have used "\n" this is nothing but a escape sequence and is known as newline character and it causes the cursor to move to the start of the next line.
Back to content
Linux has a compiler named CC ( The C Compiler ) for compiling the C programs written in a text editor. Most unix or linux version also contain The GNU Compiler Collection (GCC) which is a free C compiler that translates C into machine language that a processor can understand.A compiler converts a high-level language into machine language.
How to compile C program in Linux:
Note : The followed command should be issued carefully in terminal and keep in mind of the directory in which you are working. i.e if you have saved your file in media directory then in terminal you should type cd /media to go to that directory and then issue the following command. If you launch terminal from your taskbar or menu then the working directory is by default /home/username. It would be a better idea to open your terminal from the same directory in which your file is saved.
For compiling a C program you can simply type :
cc firstprogram.c or
gcc firstprogram.c
Now what does the above two statement does :
This will try to compile
firstprogram.c and if successful will produce a runnable file called "a.out" which is a default name. If you want to give the runnable file a better name you can type :
cc firstprogram.c -o firstprogram ( or cc firstprogram.c -o testprog.out )
gcc firstprogram.c -o firstprogram ( or gcc firstprogram.c -o firstprogram.out )
This will compile firstprogram.c and create a runnable file firstprogram
If you notice we have excluded the extension ".out" but not to worry with extension or without extension your runnable or executable file will work.
Back to content
UNIX or Linux also includes a very useful program called "make". Make allows very complicated programs to be compiled quickly, by reference to a configuration file (usually called Makefile). If your C program is a single file, you can usually use "make" by simply typing :
make firstprogram
This will compile firstprogram.c and put the executable code in firstprogram
"make" does the same task what cc firstprogram.c -o firstprogram or gcc firstprogram.c -o firstprogram does.
Back to content
The
-Wall option causes the compiler to warn you about legal but dubious code constructs, and will help you catch a lot of bugs very early. To use
-Wall option you can type :
gcc -Wall -c firstprogram.c or
cc -Wall -c firstprogram.c
If you want to be even more specific you can type :
gcc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c or
cc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c
The
-Wstrict-prototypes option means that the compiler will warn you if you haven't written correct prototypes for all your functions. The
-ansi and
-pedantic options cause the compiler to warn about any non-portable construct (i.e constructs that may be legal in gcc but not in other standard C compilers. We should try to avoid such features).
Note - Running the commands :
gcc -Wall -c firstprogram.c or
cc -Wall -c firstprogram.c
gcc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c or
cc -Wall -Wstrict-prototypes -ansi -pedantic -c firstprogram.c
produces object file named
firstprogram.o
So the next step is to make a runnable or executable file from the object file. To make an runnable or executale file type :
gcc firstprogram.o -o firstprogram or
cc firstprogram.o -o firstprogram
This produces an executable file named firstprogram
To create an executable file directly without producing an object file. You can type :
gcc -g -Wall -o firstprogram firstprogram.c or
cc -g -Wall -o firstprogram firstprogram.c
This produces an executable file named
firstprogram without producing an object file
firstprogram.o
Back to content
Now that we are done with writing and compiling our code. Lets run it. You can run your code by simply typing the runnable or executable file name i.e type :
firstprogram
If in case the above command show error and does not execute your runnable file then you can type :
./firstprogram
This tells the compiler that the executable file is in the current working directory.
Back to content
Now that you have executed your first C program. Tell us how do you feel by simply dropping a comment in the comment box.
More Informative post :