ALL BLOGS

FIELD NOTE 02 / C · FIRST STEPS

Compiling a
Small Bug in C.

A missing semicolon, a helpful compiler, and a calm way to make the smallest useful fix.

A compiler error can make a tiny mistake feel larger than it is. The useful move is to slow down: look at the line the compiler names, read the message, then check the line just before it.

Start with a small program

Save this as small-bug.c. It is short enough that we can keep the whole program in view:

#include <stdio.h>

int main(void) {
    int count = 3
    printf("count = %d\n", count);
    return 0;
}

There is one syntax bug: the declaration of count has no semicolon at the end. In C, a declaration like this is a statement, and statements need to be terminated.

Ask the compiler

Compile with a few warning flags turned on:

cc -std=c11 -Wall -Wextra -pedantic small-bug.c -o small-bug

The diagnostic wording depends on the compiler, but it will point near the printf line and complain about the syntax before it. That location is a clue, not always the exact place where the mistake began. The compiler was still expecting the previous statement to finish when it reached printf.

Make one change

Add the semicolon and leave everything else alone:

#include <stdio.h>

int main(void) {
    int count = 3;
    printf("count = %d\n", count);
    return 0;
}

Run the same compile command again. If it succeeds, run the new program:

./small-bug
count = 3

The compiler checked the source and produced an executable; running it checked what the program did. Those are two different steps, and keeping them separate makes it easier to tell what kind of problem you are solving.

Keep the feedback loop small

For a first debugging pass, read the file name and line number, then the diagnostic, then the nearby source. Fix one thing and compile again. A short loop gives each change a clear result, and the compiler becomes a guide through the code instead of a verdict on it.

3D environment loaded.