Introduction:
In this article we will discuss the execution of C or C++ program with real life example. The following diagram shows the execution process of C / C++ program execution:
Preprocessor
- It accept source code as input (which is in human understandable format) & gives output as expanded code.
- It provides the ability for the inclusion of header files, macro expansion, conditional compilation & line control.
- To remove white space
- Header file inclusion
- Line spicing
- Each comment is replace by a single space character & new line characters are retained.
- Hello.i means interpreter(i) – is a translator that converts the statement written in high level language into equivalent statement in machine level language one by one.
Compiler
- Compiler is a program which is used for translating source code from high level language (C,C++) to lower level language (assembly language, machine language).
- In this case we consider that output is in assembly language having extension .asm
- Code generated by compiler is dependent means translate source code to assembly code.
- Parsing – to check whether semicolon is missing or not.
- Tokenizing - to create meaningfully statements a++ => a+1.
- Syntax analysis
- Symantec analysis – to check logic
- Code Optimization
Assembler
- It is a program which translates assembly language program to an object file, which contains code in machine language.
- Object file contains machine code but still it is non executable having extension .obj
- This code is not pure executable code & it will look like the following,
101011100101000101010
010011010011101100010
110101100101010101100
Linker
- It is a program which accepts input as a object code of our program.
- It can take more than one object files & combined them into a single executable program having extension .exe
- Output of this phase is executable file ready for execution.
- Linker adds primary header over executable file which contains:
- Address of entry point
- Magic number
- Date Time stamp
Loader
- Loader is a program which is part of operating system that is responsible for loading programs.
- It places program in memory (RAM) & prepares them for execution.
- Loading a .exe file from HDD to RAM means magnetic address space to electric address space.
- In this phase we get output of our program.
Real Life Example:
- Suppose we like to order LCD TV from XYZ Company. Now suppose all TV’s are manufacture in CHINA.
- For transferring this LCD from China to India, company need to make it proper packing like thermocol (Polystyrene) & plastic cover with proper box, etc.
- After they transfer this via different transports to India & then to appropriate city i goes to the following: dealer, then wholesaler or shop and after that delivers at our door.
Program:
- #include < stdio.h >
- #include < conio.h >
- main()
- {
- printf("Hello");
- getch();
- }
Output:
Summary:
This article will help fresher candidates to understand execution of C or C++ program with real life example.