Execution Of C Or C++ Program With Real Life Example

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:

cpp

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.

    1. To remove white space
    2. Header file inclusion
    3. Line spicing
    4. Each comment is replace by a single space character & new line characters are retained.
    5. 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.
    1. Parsing – to check whether semicolon is missing or not.
    2. Tokenizing - to create meaningfully statements a++ => a+1.
    3. Syntax analysis
    4. Symantec analysis – to check logic
    5. 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:
    1. Address of entry point
    2. Magic number
    3. 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:

real life

  • Suppose we like to order LCD TV from XYZ Company. Now suppose all TV’s are manufacture in CHINA.

    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.

    lcd

    lcd

  • 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.

    transport

    transport

Program:

  1. #include < stdio.h >  
  2. #include < conio.h >  
  3. main()  
  4. {  
  5.   printf("Hello");  
  6.   getch();  
  7. }  
Output:

Output

Summary:

This article will help fresher candidates to understand execution of C or C++ program with real life example.


Similar Articles