Constants¶
Constants and Types¶
Question: which type has e.g. ``42``?
⟶ some more rules follow …
|
|
|
|
|
|
|
|
|
|
|
|
‘n’ |
|
Character Constants: Escape Sequences¶
|
“Alert” \ |
|
Backspace |
|
Formfeed |
|
Newline |
|
Carriage Return |
|
Horizontal TAB |
|
Vertical TAB |
|
Backslash |
|
Question mark |
|
Single Quote |
|
Double Quote |
|
Octal char value |
|
Hexadecimal |
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
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