1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

#define  TRIPLE(n)  ((n) * 3)
#define  print_int_expression(n, msg)  (                                              \
        printf("\n// Line %d in file %s\n// Compiled at time: %s\n// %s\n%s == %d\n", \
            __LINE__,                                                                 \
            __FILE__,                                                                 \
            __TIME__,                                                                 \
            (msg),                                                                    \
            (#n),                                                                     \
            (n)) )
// ∙ You can continue a preprocessor directive by putting a backslash at the end of the incomplete
//   statement.
// ∙ WARNING: Do not put a backslash at the end of the last chunk (line).

// This can ONLY be done with a #define function-like macro
// #n  is replaced with a string constant of whatever was passed for the parameter n.
// Do not add semicolon at the end of the replacement.  The semicolon will be added when
// the macro is used in the code (↓).

int main(int argc, char* argv[]) {
    //printf("TRIPLE(100) == %d\n",  TRIPLE(100));
    print_int_expression( TRIPLE(100), "basic demo of TRIPLE(…)" );
    // ⇒ ( printf("%s == %d\n", ("TRIPLE(100)"), (TRIPLE(100))) )

    print_int_expression( TRIPLE(100) + 500, "add int constant to macro result");
    // ⇒ ( printf("%s == %d\n", ("TRIPLE(100) + 500"), (TRIPLE(100) + 500)) )

    print_int_expression( TRIPLE(100) + TRIPLE(1000) + 13,  "multiple macro invocations in one expression");
    // ⇒ ( printf("%s == %d\n", ("TRIPLE(100) + TRIPLE(1000) + 13"), (TRIPLE(100) + TRIPLE(1000) + 13)) )

    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.