Explain it simply
Explain it simply
Every language has grammar, and C's is strict. A C program is made of statements, the sentences of the language, and each one ends with a semicolon the way an English sentence ends with a full stop. Related statements are grouped into blocks inside curly braces { }, and the block that belongs to main is where your program begins.
The compiler reads your file like a very literal proofreader. It does not guess what you meant: a missing semicolon, an extra brace or single quotes around a whole word is enough to stop the build. The good news is that the rules are few, and once they become habits you stop noticing them.
Problem → Why → Solution
The problem: one missing semicolon, two confusing errors
Look at this short program. The only mistake is the missing semicolon at the end of line 4. Yet GCC reports the problem on line 5, "expected ',' or ';' before 'int'", and then adds a second error on line 6, "'pears' undeclared (first use in this function)", even though pears is declared right there.
Beginners often start fixing from the bottom of the list, or rewrite line 6, which was never wrong. In bigger files a single slip can produce a screen full of messages, and chasing each one wastes time.
#include <stdio.h>
int main(void) {
int apples = 3
int pears = 4;
printf("%d\n", apples + pears);
return 0;
}Why the error appears on the wrong line
The compiler reads your code as a stream of tokens and only notices a missing semicolon when it reaches the next token that cannot fit. Here it reads int apples = 3 and then meets int on the next line, so the complaint lands on line 5. Having lost its place, it never registered pears as declared, which causes the follow-up error. Fix the first message, recompile, and most of the others usually disappear.
Core concept: how a C file is put together
Once the preprocessor has handled a .c file, the result is called a translation unit. It contains a few kinds of building blocks, and each has its own punctuation rule.
| Building block | Example | Ends with |
|---|---|---|
| Preprocessor directive | #include <stdio.h> | The end of the line, no semicolon |
| Declaration | int count = 0; | A semicolon |
| Function definition | int main(void) { ... } | A closing brace, no semicolon |
| Statement | printf("Hi\n"); | A semicolon |
| Block (compound statement) | { statement; statement; } | A closing brace |
Directives are not C statements at all: they are instructions to the preprocessor, which runs before the compiler, and each one ends at the end of its line. Everything else is free-form. Spaces, tabs and line breaks between tokens mostly do not matter, so you can split a long printf call over two lines or squeeze three statements onto one. Layout is for human readers, and good layout shows the block structure through indentation.
C is case-sensitive. printf, Printf and PRINTF are three different names, and only the first exists in the standard library. Names you invent are called identifiers: they may contain letters, digits and underscores, must not start with a digit, and cannot be one of C's keywords such as int, return or while.
Quotes carry meaning too. Double quotes make a string literal, a sequence of characters such as "Hello". Single quotes make a character constant, exactly one character such as 'H'. The compiler treats them as different types, so they are not interchangeable.
Finally, main itself. int main(void) is the standard way to write a main that takes no arguments. C17 still accepts int main(), but in a declaration empty parentheses mean "parameters not specified", so the compiler checks calls less strictly; C23 changes empty parentheses to mean no parameters. Writing void works in every standard.
- Characters become tokens
- Tokens form statements ending in ;
- Statements group into { } blocks
- Blocks form functions; main runs first
If a message makes no sense, look one token earlier: the compiler only notices a missing ; or } when the next token does not fit.
General pattern
#include <header.h> /* directive: no semicolon */
int main(void) { /* function definition */
type name = value; /* declaration */
function_call(argument); /* statement */
{ /* nested block */
statement;
}
return 0;
} /* no semicolon after a function body */Directives sit at the top of the file, one per line. Inside main's body, declarations and statements end with semicolons, while blocks open and close with braces. A function body's closing brace needs no semicolon after it.
Example: a program that shows each rule
The program keeps the classic greeting and adds four small demonstrations: a character constant, a string literal, one statement spread over two lines and a nested block. Every line it prints names the rule it shows.
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
printf("A character: %c\n", 'C');
printf("A string: %s\n", "C is fun");
printf("%s has %d letters\n",
"main", 4);
{
printf("Inside a nested block\n");
}
return 0;
}Code breakdown
- Line 1
#include <stdio.h>A preprocessor directive. It ends at the end of the line, so adding a semicolon here only earns the warning "extra tokens at end of '#include' directive".
- Line 4
printf("Hello World!\n");A complete statement: a function call followed by a semicolon. Remove the semicolon and the error is reported at the start of the next line.
- Line 5
printf("A character: %c\n", 'C');'C' in single quotes is one character, printed with %c.
- Line 6
printf("A string: %s\n", "C is fun");"C is fun" in double quotes is a string literal, printed with %s. Swapping the two quote styles would break this line.
- Lines 7-8
printf("%s has %d letters\n",One statement over two lines. The compiler only cares about tokens, and the statement ends at the semicolon at the end of line 8.
- Lines 9-11
printf("Inside a nested block\n");A block can stand on its own inside main. Its braces group statements, and later lessons use the same braces for if, loops and functions.
Hello World!
A character: C
A string: C is fun
main has 4 letters
Inside a nested blockIdentifiers and keywords
| Name | Valid? | Why |
|---|---|---|
| total_price | Yes | Letters and underscores only |
| price2 | Yes | Digits are fine after the first character |
| 2price | No | Cannot start with a digit |
| total-price | No | - is the minus operator |
| return | No | A keyword reserved by C |
| Total and total | Both | Different names, because C is case-sensitive |
Avoid names that start with an underscore followed by a capital letter, or with two underscores, such as _Count or __temp. The standard reserves those for the compiler and the library. C23 also turns some names into keywords, for example bool, true and false, so do not use them as your own identifiers either.
In real projects
How this shows up in real projects
Because the compiler ignores layout, teams agree on one and let tools apply it. clang-format can reformat a whole C codebase to a shared style in seconds, and projects such as the Linux kernel publish a coding style document covering brace placement, indentation and naming. Consistent layout matters in code review: when every block is indented the same way, a misplaced brace stands out immediately.
Real projects also spread the pieces you met today across files. Declarations (function prototypes, type definitions) go into .h header files that other files #include, and definitions live in .c files. The rule that something must be declared before it is used is exactly why headers exist. When a build in a large project fails, developers apply the habit from this lesson: scroll to the first error, fix it, rebuild.
Common mistakes
Common mistakes
1. A semicolon where none belongs
Writing #include <stdio.h>; earns the warning "extra tokens at end of '#include' directive". Worse is a semicolon right after a condition, as in if (ready); followed by a block: the semicolon is an empty statement, so the block runs every time (-Wextra warns "suggest braces around empty body in an 'if' statement"). You will meet if in a later lesson, but learn the rule now: no semicolon after a directive, a function header or a condition.
2. Single quotes around text
printf('Hello'); does not print Hello. 'Hello' is a multi-character constant, which is an int, and GCC stops with "passing argument 1 of 'printf' makes pointer from integer without a cast". Text always goes in double quotes; single quotes hold exactly one character.
3. Wrong capitalization
Printf("Hi\n"); is an unknown function, so GCC refuses to compile it with "implicit declaration of function 'Printf'; did you mean 'printf'?". The same goes for Main: a program whose only function is named Main has no main at all, so the linker fails (MinGW GCC on Windows reports undefined reference to `WinMain').
4. Unbalanced braces
Forget a closing brace and GCC reports "expected declaration or statement at end of input" at the very last line of the file, far from the real gap. Indent every block consistently, and when you open a brace, type the closing one straight away.
Mini challenge
Mini challenge: a name tag
Print a boxed name tag that uses four rules from this lesson: a string literal, a character constant, a statement split over two lines and a nested block.
- Print a top border, a name line, a grade line and a bottom border
- Use %s with a string literal for the name and %c with a character constant for the grade
- Split at least one printf call over two lines
- Put the two middle lines inside a nested block
#include <stdio.h>
int main(void) {
// TODO: print the top border, e.g. +------------------+
// TODO: open a nested block { } and inside it:
// - print "| Name: " and a name, using %s and a string literal
// - print "| Grade: " and a grade, using %c and a character constant
// (split this printf call over two lines)
// TODO: print the bottom border
return 0;
}Expected: A box like this one (your name and grade may differ): +------------------+ | Name: Aye Aye | | Grade: A | +------------------+
Show hint
Print each border as one string, and pad the name and grade lines with spaces so the right-hand | lines up with the border. Breaking the line after a comma does not end the statement; only the semicolon does.
Show solution
#include <stdio.h>
int main(void) {
printf("+------------------+\n");
{
printf("| Name: %s |\n", "Aye Aye");
printf("| Grade: %c |\n",
'A');
}
printf("+------------------+\n");
return 0;
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A C file is built from directives, declarations, function definitions, statements and blocks.
- Statements and declarations end with ;, directives end at the end of the line, and blocks end with }.
- The compiler notices a missing ; or } at the next token, so fix the first error first and recompile.
- C is case-sensitive, identifiers cannot start with a digit or be keywords, and 'A' is a character while "A" is a string.
ISO C Working Group (WG14) — ISO/IEC JTC1/SC22/WG14