LET'S TALK ABOUT C - Hello, World / Steps of compilation :
C is a compiled language.
C source files are by convention named with .c extension and we use the command “gcc” to compile C source files. (GCC stands for GNU Compiler Collection.)
Four Steps of Compilation: preprocessing, compiling, assembly, linking.
- Preprocessing is the first step. The preprocessor obeys commands that begin with # (known as directives) by:
* Removing comments
* Expanding macros
* Expanding included files
If you included a header file such as #include <stdio.h>, it will look for the stdio.h file and copy the header file into the source code file.
- Compiling:
Compiling is the second step. It takes the output of the preprocessor and generates assembly language, an intermediate human readable language, specific to the target processor.
- Assembly:
Assembly is the third step of compilation. The assembler will convert the assembly code into pure binary code or machine code (zeros and ones). This code is also known as object code.
- Linking:
Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one. If we are using a function from libraries, linker will link our code with that library function code.
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return (0);
}
At the shell prompt, enter the command “gcc main.c” and hit Enter. If it successfully compiles, the shell prompt will be displayed again. If it does not compile, it will display error message(s).
After main.c is compiled, type the command “ls” to list your directory contents and you will see an executable file named a.out.
Stay tuned for the next blog!