Unit 1 · Lesson 324 minAcademic review pending

From source code to a running C program

Read the anatomy of a C program and distinguish compile-time, link-time, run-time, and logical errors.

Choose explanation

After this lesson

You should be able to

  • Explain preprocessing, compilation, linking, and execution.
  • Classify common failures and choose the right debugging response.
01

The smallest useful program

A C program begins execution in main. The include line makes the declarations from the standard input/output header available. printf calls a library function, and returning 0 signals successful completion to the operating system.

The compiler checks syntax and types and produces object code. The linker combines object code with required libraries into an executable. Keeping these stages separate helps you read error messages instead of guessing.

hello.c
#include <stdio.h>

int main(void) {
    printf("Hello, C!\n");
    return 0;
}
02

Four kinds of trouble

A syntax error breaks the language grammar. A link error means a required definition could not be connected. A run-time error occurs while executing, such as invalid memory access. A logical error completes normally but produces the wrong answer.

The compiler cannot prove your intention. If you write area = length + width, valid syntax cannot tell it that multiplication was intended. Tests and dry-runs find logical errors.

Try it yourself

A program compiles and runs, but the average of 2 and 3 prints as 2. Classify the error and suggest the likely cause.

Need a hint?

The program completed, so focus on the expression and data types.

Check the worked solution

It is a logical error, likely integer division in (2 + 3) / 2. Use 2.0 or convert one operand to a floating type.

Quick check

Which stage combines object files and library definitions into an executable?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

C program development · Compilation · Syntax and logical errors · Object and executable code · main

Maps to course outcomes CO2, CO3.