Constants

Constants and Types

Question: which type has e.g. ``42``?

⟶ some more rules follow …

42, 052, 0x2A, 0b010010

int

42l, 42L

long

123.456f, 123.456F

float

123.456

double

123.456l, 123.456L

long double

'a', '\141', '\x61'

char

‘n’

char

Character Constants: Escape Sequences

\a

“Alert” \

\b

Backspace

\f

Formfeed

\n

Newline

\r

Carriage Return

\t

Horizontal TAB

\v

Vertical TAB

\\

Backslash

\?

Question mark

\'

Single Quote

\"

Double Quote

\ooo

Octal char value

\xhh

Hexadecimal char value

String Constants

String: array of characters, terminated by null-byte

char hello[] = "hello,world\n";
char hello[] = "hello," "world\n";
char hello[] = "hello,"
   "world\n";
  • Concatenated by compiler

  • ⟶ String literals may span multiple lines

../../../../../../_images/01-01-string1.svg

Character vs. String Constants

Easily confused:

if ('x' == "x") { /* compiler error */
    ...
}
  • 'x' is a character

  • "x" is a character array (a string)

Symbolic Constants (1)

Preprocessor constants: the good old way to express symbolic constants

#define JAN 0
#define FEB 1
#define MAR 2
...
  • Preprocessor replaces all occurences in text

  • Often not desired

    • too brutal/stupid

    • alternative: manual maintenance of values ⟶ error prone

Symbolic Constants (2)

Enumeration is often more appropriate

enum month {
    JAN,
    FEB,
    MAR,
    ...
};
  • Value has integer type

  • Value is irrelevant, only comparison is

  • switch statement