1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

//#define times_5(n) n + 5    // WRONG
//#define times_5(n) (n * 5)  // BETTER, but still WRONG
#define times_5(n) ((n) * 5)  // GOOD ★

// RULE:  When writing a #define function-like macro that returns anything:
// ∙ Parenthesize every parameter (i.e., n).
// ∙ Parenthesize the entire RHS expression.

int main(int argc, char* argv[]) {
    int n = times_5(5 + 100);  // expect 525
    // After preprocessor, this line ↑ turns to this:
    // int n = ((5 + 100) * 5)
    // … so we will have n == 525 which is correct

    printf("n == %d\n", n);
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.