Loading...
Loading...
00:00:00

What is Compiler and How it works?

How C Program Compile And Run?

Let's Understand how to C program compile and convert into executable exe file.

  1. Source code (.c file) - C language code written by programmer
  2. preprocessor - The preprocessor(preprocessor directive) provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control. it process the source code and convert into one file for compilation.
  3. Compiler - It converts written code in assembly code for Assembler. It is low level instruction.
  4. Assembler - Assembler is convert the assembly code into object file and it is used by linker.
  5. Linker - Linker is translate the object file(.o) into executable (.exe) file, It is called machine code instruction and this file is actually run by the user by double click.
  6. Finally we got Filename.exe file , In this whole process filename will not change only extension change accroding to the situation.
    Process of Compilation of C Program
    Process of Compilation of C Program

When a C program is executed, it goes through several stages, which are collectively called the "C program execution process". Here are the general steps that a C program goes through when it is executed:

  1. Preprocessing: In the preprocessing stage, the C preprocessor scans the source code and performs some text substitutions and macro expansions. For example, it may include header files, expand macros, and perform conditional compilation.

  2. Compilation: In the compilation stage, the C compiler takes the preprocessed code and translates it into assembly code or machine code, depending on the target platform. The output of this stage is an object file, which contains the compiled code and some additional information.

  3. Linking: In the linking stage, the linker takes one or more object files and links them together to form an executable program. The linker resolves external references, merges object files, and performs some additional checks and optimizations.

  4. Loading: In the loading stage, the operating system loads the executable program into memory and prepares it for execution. This may involve allocating memory, initializing variables, and setting up the program's execution environment.

  5. Execution: In the execution stage, the CPU executes the instructions in the program, starting with the main() function. The program may interact with the user, perform calculations, read and write files, and interact with the operating system.

At each stage of the execution process, the C program can encounter errors or exceptions that cause it to terminate prematurely. These errors may be due to syntax errors in the source code, missing libraries, memory errors, or other issues. To debug these errors, programmers use tools such as debuggers, profilers, and log analyzers.

Console App In C Language

After Compilation process complete, C language compiler will create a .exe file that is Console based application. Console app is look like basically traditional Command Prompt Windows that is Black Console Window. We cannot use mouse. All input operation using keyboard on console and output will be on console. To Run This console based app simply double click and run it. Using CMD, You can locate the file and type app name it will run the application. Your Application is look like this:

Console bases Application in C
Console bases Application in C