How to compile C program in linux

Introduction:

Have you wonder how C program gets compile and what are the steps involved. C program is written in user readable language and stored in file. As this file is not understood by the system hence we need a way to convert this file into machine readable format so that we can execute the our program. To make our life easy compiler comes into the picture which handles necessary things to make program executable on the system.

Compiler Dependency 

Yes compiler depends on the hardware and operating system which you are using it. Different operating system will have different set of instructions to make program executable.

Hence Linux has gcc compiler which makes programmer’s life easy.

What is compiler

As we said, compiler is the tool which process the compilation. Compilation is the process of converting source code into the machine readable code. This code is read by ALM (Arithmetic Logic Units) and CPU executes the instruction as per the code which in results our program is executed.

How to compile C program ?

Step 1 :  Writing Source Code and saving it as .c file

Let’s say you have written a program and saved into “program.c”

Here .c is file extension which gives clear understanding that this file has C program content.

create c program
C basic program
 
Step 2 : Compile using gcc compiler

As we are writing C program on linux and intends to run it on linux, we have to use suitable compiler for linux only. GCC is open source C compiler for linux operating system.

gcc compiler
 
Step 3 : Check if executbale file is generated or not

Once compilation completed which means that there is no error while compiling. gcc convert the ‘program.c’ into executable file as ‘program’. Where “-o program” implies that keep the executable file name as “program”. By default if no “-o” provided then executable file name will be “a.out” 

As gcc compiler doesn’t throw any errors which means our program has successfully compiled. 

Compiler has created ‘program’ as executable file.

c program executable
a.out executable

 

Step 4 : Execute the C program

To execute the above code type ‘./program’ and press enter. It will execute the program. Here “Hello World” will be printed on terminal.

‘./a.out’ will be used to execute the C program if no “-o” used while compiling the C program.

how to execute c program

 

How gcc throws error and how to resolve them ?

Step 1 : Compile the Code
Step 2 : Observe if any error reported.
Step 3 : Compiler error is shown with line number and error details.
Step 4 : In this case error shows as “program.c:2:26 error : ‘str’ undeclared (first use in this function)”
Step 5 : Goto line number and resolve the error. Declare ‘str’ and define it.
Step 6 : Recompile the code
Step 7 : Warning shown by compiler.
Step 8 : Execute the C program
Step 9 : Program is executed without fixing the warning part. It is advisable to fix the warning to avoid future issues.

compiler error
fix the errors reported by compiler

 

Programming Advice 

It is best practice to use ‘gcc’ compiler because it gives more control for compiling your code. To be great programmer what you need to learn is about controlling the things by your own. You should have full control on what you are developing. Hence C is best language to learn for this feature. 

If you feel you have learn something new, or in any way this articled helped you then please share this article with your techie friends and help them. 

Also if you feel what we are doing is right way then please donate us to help you in better way.