Executing a C Program

Learning computer programming is a little bit different thing than learning other things. As we all know a Computer is just a machine and we write instructions for the computer to execute and get a particular result. It is not better just to remember the syntax and rules of programming language. We must visualize and understand the concept that how computer treats your instructions in their memory and processor.

The computer understands only the instructions that are at the machine level. That is only the series of 0’s and 1’s. Also, the 0’s and 1’s series follows some pattern. So what? We are going to write the instruction with 0’s and 1’s? NO!!!. We can but it is very difficult to write programs using only 0 and 1.

That is the reason computer scientists invented and take into practice the concept of programming language. We write programs nearly in the human-readable format following some programming language-specific conventions. To execute such programs written using programming languages into a computer must be first converted to machine-readable format. So, here comes another agent/tool/software that is responsible for converting such human-readable codes into machine-readable. There are different types of agents to convert to machine-readable format. Some of them are compiler, interpreter, and assembler. But we only talk here about Compiler. Since C Programming language is Compiled type programming language. Interpreters and Assemblers are used for other types.

We are talking about the C Programming language and we will talk about how C Programs written in human-readable format are converted to machine-readable. we will discuss frequently used terms in the C Programming language first.

Executing a C Program involves a series of steps.

  • Creating a Program.
  • Compiling the Program.
  • Linking the program with the functions that are needed from C Library.
  • Executing the Program.

Some useful terms

  • Program – This is the sequence of well-organized statements to solve a praticular problem.
  • Source Code – This is the program written in human readable format.
  • Machine Code – The program that is in machine readable/binary format is called machine/object code. The compiler converts source code into machine code.
  • High level language – Program that is in human readable format.
  • Low level language – Program that can only understand by machine/computer.
  • Compiler – A program or software that converts high level language(source code) into machine level language (machine code) is called compiler. And the process of converting high level language into machine level language is called compilation process.
  • Linker – This is the program or software which helps to link object modules of program into a single object file. It also links the particular module to a system library. And the process is called linking process.

The figure below illustrates the compilation and linking process in C Programming.

Compilation of multiple c files of the program

At first, all the source files that are part of the program go through a pre-processor. After the pre-processor, the files are compiled by the compiler. After pre-processing and compiling, files are then converted to their respective object/machine codes. Now, all the object files are linked by a linker including system libraries to output the final executable code.

Now, let’s talk about preprocessing.

Preprocessing and preprocessor directive in C

Before compilations, each file goes through preprocessing by the pre-processor. Line of code that starts with # are preprocessor directives. Some examples of pre-processor directives are #define, #include, #ifndef, #endif, and so on. The code below is pre-processed by the pre-processor before compilation.

#include <stdio.h>
#define PI 3.1416

Macro

Macro is a shortcut name given to some expression. In the above example, PI is macro meaning that the shortcut name for 3.1416 is PI. So, during preprocessing wherever the PI is found in the program, the preprocessor will replace with the value of PI by 3.1416. We have to know that 3.1416 is not the value of PI but is the replacement during preprocessing. The concept of preprocessing and compiling is two different concepts.