Basic Elements of the C Language
Every C program is built from the same small set of building blocks: characters, tokens (keywords, identifiers, constants, operators, separators), comments, statements and blocks. Knowing their names and rules makes compiler messages much easier to understand.
Structure of a C program
The smallest complete program has a main function – the place where execution starts:
#include <stdio.h> /* library with printf and scanf */
int main(void) { /* execution starts here */
printf("Hello, world!\n");
return 0; /* 0 means: the program finished correctly */
}
| Part | Meaning |
|---|---|
#include <stdio.h> | preprocessor directive – includes declarations of input/output functions |
int main(void) | main function; int is the type of the value it returns |
{ ... } | block – a group of statements |
printf(...); | statement; every simple statement ends with a semicolon |
return 0; | ends main and reports success to the operating system |
Tokens: the words of the language
The compiler first splits the source code into tokens – the smallest meaningful units. There are five kinds:
| Kind | Examples |
|---|---|
| keywords | int, double, if, else, for, while, return, void |
| identifiers | sum, maxValue, count_2 – names chosen by the programmer |
| constants (literals) | 42, 3.14, 'A', "text" |
| operators | + - * / % = == < && ++ |
| separators | ; , ( ) { } [ ] |
In the statement sum = a + 5; the tokens are: sum, =, a, +, 5 and ;.
Rules for identifiers
- they may contain letters of the English alphabet, digits and the underscore
_; - they must not start with a digit (
2sumis wrong,sum2is fine); - a keyword cannot be used as a name (
int for;is an error); - C is case sensitive:
sum,SumandSUMare three different names.
Choose names that say what the variable holds: numberOfStudents is better than n2.
Whitespace and comments
Spaces, tabs and new lines (whitespace) separate tokens; the compiler ignores extra whitespace, so we use it to make code readable. Comments are text for people – the compiler skips them:
#include <stdio.h>
int main(void) {
// a one-line comment lasts until the end of the line
int a = 7, b = 5;
/* a multi-line comment
can span several lines */
printf("%d\n", a * b); /* a comment can follow code */
return 0;
}
Statements and blocks
A statement is an instruction for the computer: a declaration (int x;), an assignment (x = 5;), a function call (printf(...);) or a control statement (if, for, while). Curly braces join several statements into a block, which is treated as one statement:
#include <stdio.h>
int main(void) {
int x;
scanf("%d", &x);
if (x > 0) { /* block: two statements run together */
printf("positive\n");
printf("square: %d\n", x * x);
} else
printf("not positive\n"); /* a single statement needs no braces */
return 0;
}
- Forgetting the semicolon is the most common error; the compiler often reports it on the next line.
mainis written in lower case –Mainis a different name and the program will not link.- A comment started with
/*must be closed with*/, otherwise the rest of the file is a comment.
