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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// free to adapt/copy code in this file
#define TRIPLE_BUG(n) (n * 3)
#define TRIPLE(n) ((n) * 3)
#define PRINT_INT(n) printf("%s == %d\n", (#n), (n))
#define MULTIPLY(l, r) ((l) * (r))
int main(int argc, char* argv[]) {
// expected: 300
printf("TRIPLE(100) == %d\n", TRIPLE(100));
// expected: 600, actual 400
printf("TRIPLE_BUG(100 + 100) == %d\n", TRIPLE_BUG(100 + 100));
// expected: 600
printf("TRIPLE(100 + 100) == %d\n", TRIPLE(100 + 100));
// expected: 900
printf("TRIPLE(100 + 100 + 100) == %d\n", TRIPLE(100 + 100));
// expected: 900
PRINT_INT(TRIPLE(100 + 100 + 100));
// can pass anything into a macro,
// even if the result is invalid C code
// PRINT_INT(missing_variable);
// expected 9 * 5 = 45
PRINT_INT(MULTIPLY(9, 5));
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.