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 */
}
PartMeaning
#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:

KindExamples
keywordsint, double, if, else, for, while, return, void
identifierssum, 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 (2sum is wrong, sum2 is fine);
  • a keyword cannot be used as a name (int for; is an error);
  • C is case sensitive: sum, Sum and SUM are 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;
}
Common mistakes and tips
  • Forgetting the semicolon is the most common error; the compiler often reports it on the next line.
  • main is written in lower case – Main is 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.