typestar

Constants, macros, enums in C

The several ways C names a fixed value.

#define MAX_USERS 100
#define SQUARE(x) ((x) * (x))

enum Direction { NORTH, EAST, SOUTH, WEST };

int capacity(void) {
    const int base = 10;
    enum Direction heading = EAST;
    int area = SQUARE(base);
    return area + MAX_USERS + heading;
}

How it works

  1. #define substitutes text before compiling.
  2. A function-like macro like SQUARE inlines an expression.
  3. enum names a set of related integer constants.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
233
Tokens
50
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 38 seconds.

Type this snippet

Step 3 of 7 in Variables & types, step 3 of 35 in Language basics.

← Previous Next →