Chapter 01 of 24

1. What C is and how a program runs

Start with the basic idea

A computer follows instructions, but it does not understand the C text that you type. Your source file is a human-readable recipe; a compiler translates that recipe into a program the computer can run.

  1. Write instructions in a .c source file.
  2. Ask a compiler to check and translate the file. Errors here usually mean the written instructions do not follow C's rules.
  3. Link the translated parts and libraries into an executable, then run it.

Keep these stages separate when learning: editing changes the recipe, compiling checks it, linking assembles the program, and running performs its instructions. The rest of this chapter explains what happens at each stage.

C is a compiled, general-purpose language. It is widely used for operating
systems, embedded software, compilers, libraries, and applications where
predictable control over resources matters.

The usual path from source code to a running program is:

  1. Preprocessing: handles directives such as #include and #define.
  2. Compilation: checks and translates each source file.
  3. Assembly: turns compiler output into machine instructions.
  4. Linking: combines object files and libraries into an executable.
  5. Execution: the operating system starts the program.

The C language standard specifies language and library behavior. A compiler
also targets a platform (processor and operating system), and may offer
extensions beyond the standard. Start with standard C and learn extensions
only when you need them.

From source code to a running program

The compiler does not normally run your .c file directly. It translates
source text into lower-level representations, then a linker joins the compiled
parts and needed libraries. If a program uses several .c files, each file is
compiled and the resulting object files are linked together. This explains
why some mistakes are reported while compiling (for example, a misspelled
keyword), while others appear at link time (for example, a function was
declared but no definition was linked).

The C standard version is a contract between your program and the
implementation. Compiler defaults can change, so selecting a standard
explicitly makes a project more predictable. Platform APIs are separate:
standard C does not define every operating-system feature.

For example, these GCC commands show the translation stages for one source
file. The same source can be compiled to an object file first, then linked:

gcc -std=c17 -Wall -Wextra -Wpedantic -c hello.c -o hello.o
gcc hello.o -o hello

The first command compiles without linking; the second links the object into
an executable. Other compilers use different command-line options.

Checkpoint: In your own words, distinguish source code, a compiler, a
header, a library, and an executable. Identify which step would likely report
a missing function definition.

Core vocabulary

This small example shows a declaration, a definition, and a call:

int double_value(int value);  // declaration 

int double_value(int value)   // definition 
{
    return value * 2;
}

int main(void)
{
    int result = double_value(5);  // call; result is 10 
    return result == 10 ? 0 : 1;
}

Suggested topics

0% read