How does a C Program Execute?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Whenever a C program file is built and run, The online C compiler usually creates specific files with the same name as the C program file but with different extensions. Now let us discuss what are the steps in the execution of the C program.

What are the Steps in the Execution of the C Program?

Below given the image of the steps of execution of the C program into the C compiler.

PREPROCESSOR EXECUTION

Now let us discuss the steps of execution of the C program in detail :

C code: When you first write the code in the C language, that source code is sent to the Preprocessing section.

Preprocessing: In this section our source code is attached to the preprocessor file. Different types of header files are used like the studio.h, math.h, etc. Our C source code is attached to these types of files and the final C Source generates. (some of the preprocessor directives are #include,#define). After generating the preprocessed C source code, the C source code is sent to the compiler section.

Compiler: The preprocessed source code moves to the compiler, and an assembly-level code is generated by the compiler after the compilation of the whole C source code program. All the different files which have the C program must be saved with the .c extension. For the compiler to understand whether the program file is a C program file or not, it is necessary to have the '.c' extension. Suppose there is a program file named as first.c, The file first.c will be the source file which will consist of the C source code of the program. Now, when the file is compiled, the first thing the C compiler does is to search for any error. If there is no error, the C compiler will report for no error, after that the compiler will store the file as a .obj file of the same name, which is termed as the object file. So by this process, the compiler will create the first.obj. Although this .obj file will not be executable. After the compilation, the process is continued by the assembler section.

Assembler: This part usually generates the Object code, after taking the assembly-level code from the compiler. This object code is quite similar to the machine code or the set of binary digits. After this assembler part, The Linker continues the process, producing an executable.exe file at the end.

Linker: Before getting started with this, we should know that the library functions are a part of the C software but not of any C program. Hence, the compiler has no idea about the working of the function, whether it is a printf function or scanf function. The information for each of these functions is kept in the corresponding library, which the compiler ought to be able to connect. The linker does this task. So, when the #include is written, it includes the studio.h library, which is basically used for giving access to the Standard Output and Input. The basic goal of the linker is to link the object file to the library functions so that the programme may be run as an executable file (.exe). In this Linker process, the first.exe file will be created and this file is in an executable format. After this process, the next step is the loader process.

Loader: Whenever the command is given for the execution of a particular program, The loader plays an important role. With the help of the loader, the .exe file is loaded in the RAM and the CPU is informed of the starting point of the address of the program where it is loaded.

CPU REGISTERS

Let us understand the above diagram in detail:

Instruction Register: The current instructions which are executed by the CPU, is held by this.

Program Counter: The address of the next instructions which is to be executed by the CPU, is held by this.

Accumulator: The accumulators usually store all the information which are related to the calculations.

The first instruction is informed by the loader to the Program Counter, and after that, the execution is initiated, and the Program Counter handles the task.

Syntax :

Let us look at the image syntax of the steps of execution of C program.

PREPROCESSING

Example

Now let us look at the example of the execution of the C programming language.

Abstract:

In this code example, we will see the execution of any C program code step by step.

Code:

Output:

Explanation:

Let us now understand how we can execute this C program step by step :

  • Step 1: At first we will save this code as helloworld.c (make sure to add the c extension).
  • Step 2: Then we will open the command prompt or terminal.
  • Step 3: Then we will go to the current directory where the code is saved. We can change the directory with the cd command.
  • Step 4: After that we will compile the C code with the help of this command:

The above command will compile the C code file, and create an executable file. In the case of the windows system, it will create an a.exe file, and in the case of the Linux system, it will create an /a.out file.

  • Step 5 : Finally the C program file is executed.

Learn more about C Programming Tutorial

To learn more about C programming tutorials, you can refer to C Programming Tutorial.

Conclusion

In this article, we learned about the execution of the C program. Let us recap the points we discussed throughout the article:

  • Whenever a C program file is built and run, The C compiler usually creates specific files with the same name as the C program file but with different extensions.
  • The steps of execution of C program are C code then Preprocessing then Compiler then Assembler then Linker then Loader.
  • In Preprocessing source code is attached to the preprocessor file. Different types of header files are used like the studio.h, math.h, etc.
  • In Compiler The preprocessed source code moves to the compiler, and an assembly-level code is generated by the compiler after the compilation of the whole C source code program.
  • In Assembler it usually generates the Object code, after taking the assembly-level code from the compiler.
  • In Linker the object file is linked to the library functions and the program becomes a .exe file.
  • In Loader the .exe file is loaded in the RAM and the CPU is informed with the starting point of the address of the program where it is loaded.
  • Then we have seen an example of the execution of a C program code.