Statements, Semicolons, and Whitespace: How the Compiler Reads Your Code

So far your programs have one real instruction, a printf. In this lesson you add a few more lines, then break them on purpose to see how the compiler actually reads your code. By the end you will know why C needs all those semicolons, why an error message sometimes points to the “wrong” line, and why neat layout is really for humans.

In this lesson you will

  • Learn what a statement is, and why most statements end with a semicolon
  • Read a compiler error, including why it may point to the line after your mistake
  • Tell the difference between an error and a warning
  • See why the compiler ignores most spaces and line breaks, and why you should not

Try it: add a few statements

Open your project and add three lines above the printf. The first creates a whole-number variable called x holding 5. The second creates y without a value, and the third then gives y the value 6. (Variables are covered properly in the next lesson. For now, treat them simply as lines of code.)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 5;
    int y;
    y = 6;
    printf("Hello, my name is Ryu\n");
    return 0;
}

Build after each new line you type. It is a good habit, because small changes are easy to check. The program builds and runs, but the build log now shows something new. GCC reports messages like these, and Code::Blocks shows similar ones in its build log:

warning: unused variable 'x' [-Wunused-variable]
warning: variable 'y' set but not used [-Wunused-but-set-variable]

These are warnings, not errors. The program was still built and still runs. A warning means “this is allowed, but you probably meant something else”. Here the compiler is right: x and y are created but never used. The next lesson puts them to work.

Break it on purpose: delete a semicolon

Remove the semicolon at the end of int x = 5; (line 6) and click Build. Observe: the mistake is on line 6, but the error is reported on line 7:

main.c:7:5: error: expected ',' or ';' before 'int'

You may also see a follow-on error, 'y' undeclared. Because the compiler got confused at line 7, it never understood that y was being created. Put the semicolon back and build again, and the errors disappear.

Now remove the semicolon after y = 6 (line 8) instead, and build:

main.c:8:10: error: expected ';' before 'printf'

This time the line number points at the line with the missing semicolon. Notice the wording, though: before ‘printf’. The compiler only realised something was missing when it reached the next piece of code, the printf on the following line. Code::Blocks shows similar messages in its build log.

What just happened

A C program is made of statements. Each one is a single instruction, such as “create x and store 5 in it” or “print this message”. In C, most statements end with a semicolon. The semicolon is how the compiler knows where one statement stops and the next begins, a bit like a full stop at the end of a sentence.

The compiler reads your code from left to right, top to bottom, one piece at a time. When a semicolon is missing it does not know anything is wrong yet. It keeps reading, and only when it meets something that cannot possibly continue the statement (here, int or printf) does it stop and complain. Habit: when an error points at a line that looks fine, check the end of the line above it.

Try it: the whole program on one line

Now join the lines. Put the cursor at the end of each line and press Delete until everything sits on one line:

#include <stdio.h>
#include <stdlib.h>

int main() { int x = 5; int y; y = 6; printf("Hello, my name is Ryu\n"); return 0; }

Build and run. It works exactly as before: no new errors, and the same output. The compiler does not care about line breaks or extra spaces between the pieces of your program. What it does care about is the semicolons that end statements and the curly braces { } that group them.

Break it on purpose: a missing semicolon on one long line

On the one-line version, delete the semicolon and the space after the 5, so that 5 and int touch: int x = 5int y;. Build it.

main.c:4:22: error: invalid suffix "int" on integer constant

Observe: every error now reports the same line number, because there is only one line. The first message is new and strange, too. The compiler read 5int as one odd-looking number. Finding the mistake is now much harder, because you have to read along a long line instead of jumping straight to the right place. Press Ctrl+Z to undo, then build again to check that everything works.

Name it: statements, tokens and whitespace

The compiler first breaks your code into tokens, the smallest pieces that mean something. In int x = 5; the tokens are int, x, =, 5 and ;. (AI tools also talk about “tokens”, and the idea of chopping text into small pieces is similar.) Tokens are grouped into statements, and statements are grouped into blocks with braces.

Whitespace (spaces, tabs and line breaks) mostly just separates tokens, so the compiler is happy with one long line or with a very spread-out program. Two exceptions are worth remembering:

  • Inside a string literal, every space counts. "Hello" and "H e l l o" print differently.
  • You still need at least one space where two words would otherwise merge. int x declares a variable, but intx is a single unknown name, and GCC reports 'intx' undeclared.

So why bother with neat layout? Because people read code too: classmates, teachers, and you in two weeks’ time. Put one statement on each line, line up your braces, and indent the code inside them. You have just seen how much faster a mistake is to find when the code is laid out for humans.

Key terms

  • Statement: a single instruction in a C program. Most statements end with ;.
  • Semicolon (;): marks the end of a statement.
  • Token: the smallest meaningful piece of code, such as a keyword, name, number, operator or punctuation mark.
  • Whitespace: spaces, tabs and line breaks. Outside string literals it mostly just separates tokens.
  • Syntax error: code that breaks the rules of C. The program will not build.
  • Warning: something legal but suspicious. The program still builds.

Quick check

  1. GCC reports error: expected ',' or ';' before 'int' on line 7, but line 7 looks correct. Where should you look?

    Show answer

    At the end of line 6. A semicolon is probably missing there, and the compiler only noticed when it reached int on line 7.

  2. Your program builds with two warnings and no errors. Can you run it?

    Show answer

    Yes. Warnings do not stop the build, but read them, because they often point to a real mistake.

  3. Will a C program still compile if every statement inside main is on a single line?

    Show answer

    Yes, as long as the semicolons and braces are correct. It is just much harder for people to read and debug.

  4. Why does intx = 5; fail while int x = 5; works?

    Show answer

    Without the space, intx is read as one token, a name that has never been declared. The space separates the keyword int from the name x.

Optional: a recorded demonstration of these steps starts around 27:20.

Part of the Introduction to C series. Next: Variables and Format Specifiers: Printing Values with %d

Leave a Reply