Pre-Coding Foundations

Compilers, Terminals & Command Line Basics

Learn what the terminal prompt is and how GCC translates source code into executable binary files.

1. The Hook Have you ever tried speaking English to someone who only understands French? You would need a translator standing between both of you to translate line-by-line or paragraph-by-paragraph.

Your C source code is human language. Your CPU only speaks machine electrical signals. How do they communicate?


2. Real World Story Before smartphones and graphical windows existed, programmers interacted with computers purely through text prompts called Terminals.

You typed a command -> hit Enter -> computer responded with text output.

Understanding the terminal gives you super-powers as a computer engineer!


3. Problem How do we convert a file written in C text (main.c) into an executable file (a.out) that the CPU can execute?


4. Concept: The 4 Stages of Compilation When you run gcc main.c -o myprogram, 4 distinct stages occur:

1. Preprocessing: #include files are expanded, comments removed. 2. Compilation: C text converted to Assembly instructions (main.s). 3. Assembly: Assembly instructions converted to Machine Code Object bytes (main.o). 4. Linking: Object code joined with system libraries (printf) to produce the final runnable binary (myprogram).


5. Mental Model - Source Code (`main.c`) = Recipe written in English. - GCC Compiler = Professional Chef Translator. - Executable (`a.out`) = Prepared Dish ready to serve to CPU.


6. Visualization

main.c  ---> [1. Preprocessor] ---> main.i ---> [2. Compiler] ---> main.s ---> [3. Assembler] ---> main.o ---> [4. Linker] ---> myprogram (Executable)

7. Code: Terminal Command Blueprint

// 1. Compile main.c into an executable named myprogram:
// gcc main.c -o myprogram

// 2. Run the executable in terminal: // ./myprogram ```


8. Explain EVERY Command - pwd: Print Working Directory (where am I?). - ls: List files in current directory. - cd folder: Change directory. - gcc: GNU Compiler Collection command.


9. Dry Run 1. Type gcc main.c -o myprogram. 2. Preprocessor expands header. 3. Assembly generated. 4. Binary executable myprogram created. 5. Type ./myprogram -> Output printed to terminal screen!


10. Memory & File State Table

File Name     Type                  Human Readable?    Purpose
---------------------------------------------------------------------------------
main.c        Source Code           Yes                Your original C code
main.s        Assembly File         Semi               CPU assembly instructions
main.o        Object File           No (Binary bytes)  Unlinked machine code
myprogram     Executable Binary     No (Binary bytes)  Runnable program for OS

11. Common Mistakes - Typing ./main.c directly instead of compiling it first with gcc. - Forgetting to specify -o output_name, causing GCC to generate the default file name a.out.


12. Interview Corner - Q: What is the difference between a Compiler and an Interpreter? - A: A Compiler translates the entire program into machine code beforehand. An Interpreter translates and executes code line-by-line at runtime.


13. Practice - Easy: Use terminal command ls to list files. - Medium: Create a folder named c_practice using mkdir and move into it. - Challenge: Compile a C file manually using GCC and specify a custom executable output name.


14. Interactive Idea - Interactive Compiler Pipeline Visualizer: An animated step-by-step UI widget showing a C file passing through Preprocessor, Compiler, Assembler, and Linker stages!


15. Summary Today you learned how GCC translates your C source code into runnable CPU machine binaries across 4 stages!