Part 1 — Chapter 2

Chapter 2: How C Programs Work

A C program is written in a human-readable form called source code. Since the computer cannot understand C directly, the code must be translated into machine instructions before execution.

This process converts the program from source code into an executable program that the processor can run.

Program Execution Flow

C Source Code
Compiler
Machine Code
Executable
Program Runs

Source Code

Source code is the program written by the programmer in a .c file using the C programming language.

example.c
C
printf("Hello, World!");

Source code is easy for humans to read and modify, but it cannot be executed directly by the processor.

Compiler

The compiler translates C source code into machine code. It also checks the program for syntax errors during compilation.

If the code is valid, an executable file is generated.

Executable Program

The executable file contains machine instructions that the processor can execute directly. When the program starts, it is loaded into memory and execution begins from the main() function.

What Happens During Execution?

  • The program is loaded into RAM
  • Instructions are fetched by the CPU
  • The processor executes instructions sequentially
  • Variables occupy memory locations
  • Output is generated and sent to devices like the terminal

At the hardware level, the CPU continuously performs a cycle of fetching, decoding, and executing instructions stored in memory.

Why This Matters in Embedded Systems

In embedded systems, programs run directly on microcontrollers with limited memory and processing power. Instead of printing output to a terminal, embedded software interacts with hardware such as LEDs, sensors, timers, communication interfaces, and memory-mapped registers. Understanding how programs are compiled, stored in memory, and executed by the processor is essential for low-level and hardware-oriented programming.

Summary

A C program moves through several stages before execution: source code, compilation, executable generation, and processor execution. Understanding this workflow builds a strong foundation for programming and embedded systems development.

← Chapter 1: Introduction to C Chapter 3: Your First C Program →