What happens when you type gcc main.c

Miguel Pacheco
3 min readFeb 17, 2021

Hello everybody, welcome to this post. In this case i going to talk about what happens when we write gcc main.c in our terminal (if you don’t remember what is a terminal check my other post)

When you want to execute code to create an object file, the computer needs to be communicated with in machine which is binary code (zeros and ones (0,1)) but, humans communicate in language higher than binary, so we need to “translate” our language (high-level language like phyton or in this case C) to machine language (binary). For to do this in our console we need a command and this command is:

gcc main.c

This is what happen when we use GCC main.c

Well let’s unpack this image, so here we go.

  1. Preprocessing: This process generates the some intermediate files that later are given to the compiler, this task is does by the preprocessor. The preprocessor does 3 task: first, removes comments from the code, including the header file (standard in all C files) into the generated file, and if macros are used, for to do this we use:
[bash]$ gcc -E HelloWorld.c -o HelloWorldOutput (where HelloWorld.c is the C file and HelloWorldOutput is the output)(using gcc's “-E” flag we can do directly the pre-processing operation)
  1. The compiler take the file (created before by the preprocessor) code and create the assembly code for to do this we use
[bash]$ gcc -S HelloWorld.i -o HelloWorld.s (in this case HelloWorld.i is the example file and HelloWorld.s is the Output with the Assembly code)(Using the "-S" we can convert the preprocessed C source code into assembly language without creating an object file).
  1. The assembler converts the assemble code into object code (or Machine code) because machines only understand binary, for to do that we use:
[bash]$ gcc -c HelloWorld.c -o HelloWorld.o (using the "-c" flag in gcc we can convert the assembly code into machine level code)

HelloWorld.o

  1. This is the last phase in which all the before steps (Preprocessing, compilation and Assemble) are converted to a executable file (.exe in Windows)
[bash]$ gcc -o Output HelloWorld.c (This command runs the file "HelloWorld.c" and produce the final executable file "Output")

listing all the files using ls-l

As we can see, ‘Output’ file by default have the permissions -rwxrwxr-x, this just means that the file has executable permission for all users (owner, group and others)

And, that’s all for today, thank you so much for read this blog and see you next time. Happy learning :)

--

--

Miguel Pacheco

Student at Holberton School | Aspiring to be a Front-End Web developer