12
What Really Happens when a C program runs?
C Programming
Runtime
concepts
helloworld
Compiler
Linker
Executable
Structure

Hey its your favourite program!! HELLO WORLD

int main() { printf("Hello World!\n"); return 0; }

Lets quickly dive into the technicalities of this hello world! program.

What happens when a C program runs? The above small piece of code which runs in matter of milliseconds goes through several entities to give you the output, which you very dearly wish to catch a glimpse of.

The C compiler's Job:

It can understand and appreciate the processes involved in preprocessing, compiling, linking, loading and running C/C++ programs.

COMPILERS, ASSEMBLERS and LINKERS

A C’s program building process involves four stages and utilizes different ‘tools’ such as a preprocessor, compiler, assembler, and linker.

At the end there should be a single executable file. Given below are the stages that happen in order regardless of the operating system and the compiler we use.

  1. Preprocessing is the first pass of any C compilation. It processes include-files, conditional compilation instructions and macros.

  2. Compilation is the second pass. It takes the output of the preprocessor, and the source code, and generates assembler source code.

  3. Assembly is the third stage of compilation. It takes the assembly source code and produces an assembly listing with offsets. The assembler output is stored in an object file.

  4. Linking is the final stage of compilation. It takes one or more object files or libraries as input and combines them to produce a single (usually executable) file. In doing so, it resolves references to external symbols, assigns final addresses to procedures/functions and variables, and revises code and data to reflect new addresses (a process called relocation).

*if you use the IDE type compilers, these processes are quite transparent.

Now we are going to examine more details about the process that happen before and after the linking stage. For any given input file, the file name suffix (file extension) determines what kind of compilation is done.

In UNIX/Linux, the executable or binary file doesn’t have extension whereas in Windows the executables for example may have .exe, .com and .dll.

Here are a few,

  • file_name.c C source code which must be preprocessed.

  • file_name.h C header file (not to be compiled or linked).

  • file_name.C C++ source code which must be preprocessed.

OBJECT FILES and EXECUTABLE

After the source code has been assembled, it will produce an Object files (e.g. .o, .obj) and then linked, producing an executable files.

An object and executable come in several formats such as ELF (Executable and Linking Format) and COFF (Common Object-File Format). For example, ELF is used on Linux systems, while COFF is used on Windows systems.

A few other file formats are

  • a.out The a.out format is the original file format for Unix.
  • COFF The COFF (Common Object File Format) action is limited.

    When we examine the content of these object files there are areas called sections. Sections can hold executable code, data, dynamic linking information, debugging data, symbol tables, relocation information, comments, string tables, and notes.

Some sections are loaded into the process image and some provide information needed in the building of a process image while still others are used only in linking object files.

For your quick reference : enter image description here

Author

Notifications

?