C Language Skill UP
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Working of C program
or
Compilation process

compilation process in c program

   Compilation process of C:
  First of all understand What is Compilation?
Compilation or Compiler:a compiler is a computer program that translates computer code written in one programming language (the source language hello.c) into another language (the target language hello.obj and hello.exe). The name “compiler” is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program.

   Step by Step Compilation process in C
Compilation Process involve Following Steps

  • Preprocessing
  • Compiling
  • Assembling
  • Linking

  • compilation process C programs

    a. Pre-Processing:
    Pre-processing is the first step in the compilation process in C performed using the pre-processor tool (A pre-written program invoked by the system during the compilation). All the statements starting with the # symbol in a C program are processed by the pre-processor, and it converts our program file into an intermediate file with no # statements. Under following pre-processing tasks are performed :
      i. Comments Removal:
    Comments in a C Program are used to give a general idea about a particular statement or part of code actually, comments are the part of code that is removed during the compilation process by the pre-processor as they are not of particular use for the machine. The comments in the below program will be removed from the program when the pre-processing phase completes.

    /* This is a multi-line comment in C */
    #includel<stdio.h>
    int main()
    {
    /* this is a single-line comment in C */
    return 0;
    }

    ii. Macros Expansion:
    Macros are some constant values or expressions defined using the #define directives in C Language. A macro call leads to the macro expansion. The pre-processor creates an intermediate file where some pre-written assembly level instructions replace the defined expressions or constants (basically matching tokens). To differentiate between the original instructions and the assembly instructions resulting from the macros expansion, a '+' sign is added to every macros expanded statement. Macros Examples:
    Defining a value
    #define G 9.8     Defining an expression
    #define SUM(a,b) (a + b)
    iii. File inclusion:
    File inclusion in C language is the addition of another file containing some pre-written code into our C Program during the pre-processing. It is done using the #include directive. File inclusion during pre-processing causes the entire content of filename to be added to the source code, replacing the #include directive, creating a new intermediate file. Example: If we have to use basic input/output functions like printf() and scanf() in our C program, we have to include a pre-defined standard input output header file i.e. stdio.h.

    #include <stdio.h>


    iv. Conditional Compilation:
    Conditional compilation is running or avoiding a block of code after checking if a macro is defined or not (a constant value or an expression defined using #define). The preprocessor replaces all the conditional compilation directives with some pre-defined assembly code and passes a newly expanded file to the compiler. Conditional compilation can be performed using commands like #ifdef, #endif, #ifndef, #if, #else and #elif in a C Program. Example :
    1. Printing the GRAVITY macro, if GRAVITY macro is defined, else printing Not Defined and ending the conditional compilation block with an #endif directive.
    #include <stdio.h> /* if we uncomment the below line, then the program will print GRAVITY in the output.*/
    // #define GRAVITY 18
    int main()
    {
       /* if `GRAVITY` is defined then print the `GRAVITY` else print "Not Defined"*/
    #ifdef GRAVITY
    printf("Gravity is %d", GRAVITY);
    #else
    printf("Not Defined");
    #endif
    return 0;
    }


    Explanation:
    #ifdef directive checks if the macro GRAVITY is defined or not, and as we have commented the #define statement the #ifdef GRAVITY block of code will not execute and control flow will move to the #else block and Not Defined will be printed on the output screen, #endif ensures that the conditional compilation block ends there.
    Now let's see the below figure that shows how a pre-processor converts our source code file into an intermediate file. Intermediate file has an extension of .i, and it is the expanded form of our C program containing all the content of header files, macros expansion, and conditional compilation.



    b. Compiling:
    Compiling phase in C uses an inbuilt compiler software to convert the intermediate (.i) file into an Assembly file (.s) having assembly level instructions (low-level code). To boost the performance of the program compiler translates the intermediate file to make an assembly file. Assembly code is a simple English-type language used to write low-level instructions (in micro-controller programs, we use assembly language). The whole program code is parsed (syntax analysis) by the compiler software in one go, and it tells us about any syntax errors or warnings present in the source code through the terminal window. The below image shows an example of how the compiling phase works.

    C. Assembling:
    To understand Assembling First we need to understand What is Assembler?
    Assembler:-An assembler is a program that takes basic computer instructions and converts them into a pattern of bits that the computer's processor can use to perform its basic operations. Some people call these instructions assembler language and others use the term assembly language.
    It takes basic Computer commands and converts them into Binary Code that Computer's Processor can use to perform its Basic Operations.
    In this assembling level the Assembly level Code(.s)file is converted to machine code(in binary or hexadecimal) using assembler.
    The file generated in this step has the same name as the assembly file known as object file with extension .obj on windows and .oin UNIX operating System. following diagram shows how the assembling step works. areaofcircle.s is translated to areaofcircle.o has same name but different Extension.

    d. Linking:
    What is Linker in c programing?
    Linker:-A linker is an important utility program that takes the object files, produced by the assembler and compiler, and other code to join them into a single executable file. There are two types of linkers, dynamic and linkage.
    What is Linking in C programing?
    Linking:
    Linking defined as a process of including library files into the program. library files are the files predefined files that has the definition of functions in the machine language, these file have an extension .lib
    The linking process generates an executable file with an extension of .exe in DOS and .out in UNIX OS.
    Below image shows how the linking works.

    Compilation Flow with diagram
    Let us understand the compilation process in detail with the help of following digaram.

    Here in above given fig. we Have C Program file areaofcircle with an extension of .c i.e.areaofcircle.c file.
    Step 1: is preprocessing of header files, all the statements starting with # hash symbol and comments are replaced/removed during the pre-processing with the help of a pre-processor. It generates an intermediate file with .i file extension i.e. a areaofcircle.i file.
    Step 2: is a compilation of areaofcircle.i file. Compiler translates the areaofcircle.i file to areaofcircle.s with assembly level instructions i.e. low-level code.
    Step 3: assembly-level code instructions are converted into machine-understandable code (binary/hexadecimal form) by the assembler. The file generated is known as the object file with an extension of .obj/.o i.e. areaofcircle.obj/areaofcircle.o file.
    Step 4: Linker is used to link the library files with the object file to define the unknown statements. It generates an executable file with .exe/.out extension i.e. a areaofcircle.exe/areaofcircle.out file. Next, we can run the areaofcircle.exe/areaofcircle.out executable file to get the desired output on our output window, i.e., Hello World!.

    C Language

    • Home
    • Why C Language
    • History of C Language
    • Applications of C Language
    • Introduction To C
      • What is Program?
      • Structure of C Program
      • Working Of C Program
      • CHARACTER SET
      • VARIABLES AND IDENTIFIERS
      • BUILT-IN DATA TYPES
      • OPERATORS AND EXPRESSIONS
      • CONSTANTS AND LITERALS
      • SIMPLE ASSIGNMENT STATEMENT
      • BASIC INPUT/OUTPUT STATEMENT
      • SIMPLE 'C' PROGRAMS
      • Assignments
    • Operators in C Programming
      • Arithmetic Operators
      • Assignment Operators
      • Increment and Decrement Operators
      • Relational Operators
      • Logical Operators
      • Bitwise Operators
      • Other Operators
      • Assignments
    • CONDITIONAL STATEMENTS
      • DECISION MAKING WITHIN A PROGRAM
      • CONDITIONS
      • IF STATEMENT
      • IF-ELSE STATEMENT
      • IF-ELSE LADDER
      • NESTED IF-ELSE
      • SWITCH CASE
      • Assignments
    • LOOPS STATEMENTS
      • Introduction to Loops
      • GO TO Statement
      • Do while Loop
      • While Loop
      • Nested While Loop
      • Difference Between While and Do while
      • Difference Between Goto and loop
      • while loop assignments
      • C FOR Loop
      • C For loop examples
      • Nested for loop
      • Nested for loop examples
      • Infinite while Loops
      • Infinite for Loops
      • Continue in Loops
      • break in Loops
      • difference while do..while & for
      • Assignments
    • Arrays
      • One Dimensional Array
      • Declaring 1D Arrays
      • Initilization of 1D arrays
      • Accessing element of one 1D Array
      • Read and Display 1D Arrays
      • Two Dimensional Arrays
      • Declare 2D Arrays
      • Read and Display 2D Arrays
      • Assignments/Examples
    • Functions
      • Introduction
      • Need For User-Defined Function
      • Multiple Function Program
      • Modular Programming
      • Elements Of User Defined Function
      • Function Definition
      • Function Declaration
      • Types of functions
      • Nesting of Function
      • Recursion
      • Passing Array To Functions
      • Scope,Visibility and Lifetime of Variables
      • Assignments
    • Structure
      • Introduction
      • Array vs Structure
      • Defining Structure
      • Declaring Structure Variables
      • Type Defined Structure
      • Accessing Structure Members
      • Structure Initilization
      • Copying & Comparing Structure Variables
      • Array of Structure
      • Arrays Within Structure
      • Structures Within Structures
      • Structures and Functions
      • Structure Examples/Assignments
    • Union
      • Define Union
      • Create and use Union
      • Difference Between Structure and Union
      • Union Examples
      • Union FAQ
    • Pointers
      • What Are Pointers In C?
      • How Do We Use Pointers In C?
      • Declaration Of A Pointer
      • The Initialization Of A Pointer
      • Syntax Of Pointer Initialization
      • Use Of Pointers In C
      • The Pointer To An Array
      • The Pointer To A Function
      • The Pointer To A Structure
      • Types Of Pointers
      • The Null Pointer
      • The Void Pointer
      • The Wild Pointer
      • The Near Pointer
      • The Huge Pointer
      • The far Pointer
      • dangling pointer
      • Accessing Pointers- Indirectly And Directly
      • Pros Of Using Pointers In C
      • Cons Of Pointers In C
      • Applications Of Pointers In C
      • The & Address Of Operator In C
      • How To Read The Complex Pointers In C?
      • Practice Problems On Pointers
    • File Processiong
      • File Handling In C
      • Types Of Files In C
      • Operations Done In File Handling
      • File Examples
      • Binary Files
      • count words,lines in a file
      • Copy files
      • Update File
      • count vowels in a file
    • Preprocessor
      • Macro substitution division
      • File Inclusion
      • Conditional Compilation
      • Other directives
      • Examples
    • Dynamic Memory Allocation
      • malloc
      • calloc
      • free
      • realloc
      • Examples
    • Storage Classes
    • Graphics
    • Frequently Asked Interview Questions (FAQ)
      • Introduction To C FAQ
      • Operators FAQ
      • Conditional Statements FAQ
      • Loops FAQ
      • Arrays FAQ
      • Function FAQ
      • Structure FAQ
      • Pointers FAQ
      • Files FAQ
      • Storage classes FAQ
      • Dynamic Memory FAQ
    • Programs/Assignments
      • Introduction To C
      • Operators
      • Conditional Statements
      • Loops
      • Arrays
      • Function
      • Structure
      • Pointers
      • Files
      • Storage classes
      • Dynamic Memory
    • Case Studies
    • Multiple Choice Questions
      • Introduction To C MCQ
      • Operators MCQ
      • Conditional Statements MCQ
      • Loops MCQ
      • Arrays MCQ
      • Function MCQ
      • Structure MCQ
      • Pointers MCQ
      • Files MCQ
      • Storage classes MCQ
      • Dynamic Memory MCQ
      • More MCQ

    Get in touch

    • tech2dsm@gmail.com

    © Sankalan Data Technologies. All rights reserved.