GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Compilation process of a C Program

Compilation is a process of converting high level programming to machine level language.

compiler

The compiler goes through the following steps to convert it to the machine code or object code.

compilation process

You may watch the video at the end of this page.

Source file

  • The source file is written with the .c extension. 
  • This file is in high level language and cannot be understood by the machine, so needs to convert to low level language. 

Pre-processor

  • This is the first pass of the compiler.
  • The pre-processor removes:
    • comments.
    • expansion of the included files.
    • expands the MACROS or mainfest constants.
    • conditional compilation.
  • The pre-processor creates the .i file.

Compiling

  • In this step the .i file is compiled to the .s file
  • The .s file is created with the filename of test.s (since test is the C file that we have created)

Assembling

  • In this step the .s file is converted to the .o (object file)
  • The object file is the one which the machine understands, and we execute the object file.

Linker

The linker links all the function calls. If any function definition is missing or there is a mismatch, then the linker reports an error.

Writing C program and executing using gcc compiler on linux terminal

We write the program using the editor. Let us use the gedit editor.

gedit test.c
creating file-c

We write the program in the file,

#include<stdio.h>
int main()
{
printf("Welcome to geteducate.org\n");
return 0;
}

To convert this file (test.c) to machine language we use compiler, gcc and we compile file as,

gcc test.c
compiling-c-file

By default, it creates an object file with name a.out

To execute the object file we write,

./a.out
executing c file

We can change the name of the object file using -o option of gcc.

gcc test.c -o test
creating object file with -o

Creates an object file with name test instead of a.out

To execute,

./test
execution with test object file

Note:

  • We execute the .o file not the .c file
  • Hence any modifications in .c file has to be recompiled to overwrite the .o file
Scroll to Top