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.
After this lesson
You should be able to
- Explain preprocessing, compilation, linking, and execution.
- Classify common failures and choose the right debugging response.
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.
#include <stdio.h>
int main(void) {
printf("Hello, C!\n");
return 0;
}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?
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.