← All posts
In progress

From High To Low

Essay ~3 min read

You write your first C program and run it. It prints “hello world! on the terminal screen and you are elated. You feel like a god. This ,however, is an introduction to pain you will later come to hate and love at the same time.

#include <stdio.h>
#define  NUM 1
// my first c program
// I am a god

int main()
{
	printf("hello world!\n");
	 printf("program number %d\n",NUM);
		
	 return 0;
}

But have you ever wondered what happens to that C program when you compile it. There is surprisingly a lot done on this high level language to turn it into bits that a computer can can execute. Lets peel the abstractions. Kindly save the program below as example.c and follow along in your code editor.

Source Code are statements written by a programmer using a high-level language such as C 1.High level languages were introduced as an abstraction layer for instructing computers to carryout tasks. They replaced the previously cumbersome way of programming which involved writing programs in binary using ones and zeros using code sheets first and then punching holes into a card to write one line of code. Peeling Abstractions

Later, after the ones and zeros, assembly language was introduced which reduced on the complexity of writing programs in pure ones and zeros by introducing opcodes that represented instructions. This was far better than binary but still left a code reusability gap since programmers had to rewrite alot of code for different programs. It was high level programming languages that changed the game. With their prebuilt in library of useful and commonly used functions, control structures, data types and English like statements, they made programming a breeze. (almost anyway) The computer however still understands ones and zeros therefore, there was need for a middle man to translate the source code into a binary form that the computer can run.

That middle man was the compiler. When you run a C program in Visual Studio Code, it is first compiled to make an executable file containing binary that the computer can work with when you run the program.

This is the gist of this blog

Below are the steps the source code goes through to become the executable file.

  1. Preprocessing
  2. Compiling (funny because the entire process is called compiling) This constitutes of the following • Lexical analysis • Syntax analysis • Sematic analysis
  3. Intermediate Representation (IR) generation and optimization
  4. Assembly
  5. Linking
  6. Loading

First things first,

1) The Preprocessing phase:

During this phase the following adjustments are made on the source code: i. Macros are expanded. This means that wherever they appear in the code, they are replaced with the value they represent. for example, NUM is replaced with its value, 1. ii. Comments are removed. (comments are for humans) iii. The contents of the all header files is included in the source file. That is declarations of functions, macros, constants, data types, and other reusable definitions

To view the appearance of the program after preprocessing, we use the terminal command

gcc   -E    first_program.c   -o    first_program.i

The above command only pre-processes the c file, the first stage of compilation and outputs a file called first_progra.i which is a preprocessed c file. Compare that preprocessed file with the original c file

Pre-processed c file

/* Thousands of function declaration from standard library omitted her*/
extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1)));


extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1)));
# 953 "/usr/include/stdio.h" 3
extern int __uflow (FILE *);
extern int __overflow (FILE *, int);
# 977 "/usr/include/stdio.h" 3

# 2 "first_program.c" 2

# 6 "first_program.c"
int main()
{
 printf("hello world!\n");
  printf("program number %d\n",1);

  return 0;
}

if you compare the sizes of the two files, first_program.c and first_program.i, you can see that the latter is alot bigger due to all those function declarations, type definitions and macro expansions from standard IO header file being placed in the file.

❰joshua❙~/blogs❱✔≻ du -h first_program.*
4.0K	first_program.c
20K	first_program.i
❰joshua❙~/blogs❱✔≻ wc -c first_program.*
  165 first_program.c
20377 first_program.i
20542 total 

If you look at the file, you will notice that it is still actually just c code and nothing much changed other than the expansion of the original file. Next comes the compilation process.

2) Compilation phase

Change Log

1 total
  1. complement #1 Return

    Is C a high-level language?

    Yes C is a high level language. Some one almost roasted on this one. But when you write C , you are not writing Assembly or machine code. You write variables, functions, control structures likes loops are provided out of the box. So syntax wise, C is high level language. But if considering control it gives the programmer, then C is a low level language compared to languages like python,JS or java. C gives the programmer almost nothing to work with. I said almost nothing. The programmer accesses the hardware directly. The programmer manages the memory themselves. C gives no array data structure. So C is more of medium level language because it gives the programmer low level control with high level syntax. The best of both worlds until you shoot yourself in the foot. Sweet!!