Language 1 of 10 · Topic 0.1

Program Structure, Header Files & GCC Compilation Pipeline

1Concept

In C, execution begins strictly at the main() function returning an integer exit status (0 for success). C relies on preprocessor directives (#include <stdio.h>, #define) to incorporate header declarations before compiling. The GCC toolchain converts C code through 4 distinct stages: Preprocessing (macro expansion & header inclusion), Compilation (C code to assembly), Assembly (assembly to machine object code .o), and Linking (resolving library symbols like printf to generate the ELF/PE executable).

2Architecture Diagram

+-------------+     +-------------+     +-------------+     +-------------+
| Source .c   | --> | Preprocessor| --> | Compiler    | --> | Assembler   | --> Linker --> [Native Binary]
| #include    |     | (cpp) .i    |     | (cc1) .s    |     | (as) .o     |
+-------------+     +-------------+     +-------------+     +-------------+

3Code Example

Stage 0 Language Foundations
#include <stdio.h>
#include <stdlib.h>

// Global constant macro
#define APP_VERSION "1.0.0"

int main(int argc, char *argv[]) {
    printf("=== C Program Execution Started ===\n");
    printf("App Version: %s\n", APP_VERSION);
    printf("Argument count: %d\n", argc);
    
    for (int i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
    
    return EXIT_SUCCESS; // Returns 0 to OS kernel
}

4Expected Output

=== C Program Execution Started ===
App Version: 1.0.0
Argument count: 1
argv[0] = ./app_main

5Key Takeaways

  • main() must return an int (0 signifies successful exit to OS shell).
  • Header files (.h) provide declarations; source files (.c) provide definitions.
  • Preprocessors perform pure text substitution without syntax analysis.