Overview
Directly writing constants into our code is referred to as using magic numbers.
- We try to avoid this lol
Example
- Bad - wtf is 52?
for (int i = 0; i < 52; i++) {
// deal card
}- Better, but introduces another problem
- What if some other function manipulates this number?
int deck_size = 52;
for (int i = 0; i < deck_size; i++) {
// deal card
}- Best
- C has a preprocessor directive (also called a macro) for creating symbolic constants
- Use preprocessor directives!
#define DECK_SIZE 52
... // some code
for (int i = 0; i < DECK_SIZE; i++) {
// deal card
}Preprocessor Directives
#define NAME REPLACEMENT
#define PI 3.14159265- At the time your program is compiled,
#definegoes through your code and replacesNAMEwithREPLACEMENT - if
#includeis similar to copy/paste, then#defineis analogous to find/replace - NO semicolon!
- Convention
- All caps to make it clear that it’s a constant and not a variable