Line number tracking
What you'll learn: How to use the compiler's error messages to pinpoint the exact location of a bug in your source code.
The compiler is your GPS
Imagine you are following a recipe to bake a cake, but the instructions are written in a messy, scrambled handwriting. If you miss a step or add the wrong ingredient, the cake won't bake. When you write C code, the compiler acts like a strict kitchen inspector. If you make a mistake—like forgetting a semicolon or misspelling a command—it won't just say "something is wrong." It will give you a specific map to the problem.
In C, the compiler reads your file from top to bottom, counting every line as it goes. When it hits a wall and can't understand your code, it stops and shouts out the line number where it got stuck.
Reading the "Map"
When you compile a program with an error, your terminal will spit out a message that usually looks like this: main.c:5:10: error: expected ';' before 'return'.
Here is how to decode that:
- Filename (
main.c): The file where the error happened. - Line Number (
5): The exact horizontal line in your text editor where the compiler tripped. - Column Number (
10): How many characters across the line the error occurred. - The Message: A description of what the compiler thinks is missing or broken.
A concrete example
Look at the code below. Can you spot the missing piece?
#include <stdio.h>
int main() {
printf("Hello, world!")
return 0;
}
If you try to compile this, the compiler will point to Line 5. Why? Because even though the mistake is technically the missing semicolon on Line 4, the compiler didn't realize anything was truly "wrong" until it moved to the next line and saw the word return instead of the ; it was expecting.
When tracking errors, always look at the line number provided, but keep an eye on the line immediately above it, too. The compiler is telling you where it noticed the trouble, which is almost always exactly where the fix needs to be.
Key takeaway
Use the line numbers in error messages as a direct pointer to where your code needs fixing.