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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// BROKEN: missing backslashes to continue macro definition
#define call_fn_with_logging(fn) do {
printf("Entering %s\n", (#fn));
fn();
printf("Leaving %s\n", (#fn));
} while(false)
void greet() {
printf("Hey, hey!\n");
}
int main(int argc, char* argv[]) {
call_fn_with_logging( greet );
// Expand to something equivalent to this:
// printf("Entering greet\n");
// greet();
// printf("Leaving greet\n");
return EXIT_SUCCESS;
}
/*
e.c:9:9: error: expected declaration specifiers or ‘...’ before string constant
printf("Entering %s\n", (#fn));
^~~~~~~~~~~~~~~
e.c:9:26: error: expected declaration specifiers or ‘...’ before ‘(’ token
printf("Entering %s\n", (#fn));
^
e.c:9:27: error: stray ‘#’ in program
printf("Entering %s\n", (#fn));
^
e.c:10:2: warning: data definition has no type or storage class
fn();
^~
e.c:10:2: warning: type defaults to ‘int’ in declaration of ‘fn’ [-Wimplicit-int]
e.c:11:9: error: expected declaration specifiers or ‘...’ before string constant
printf("Leaving %s\n", (#fn));
^~~~~~~~~~~~~~
e.c:11:25: error: expected declaration specifiers or ‘...’ before ‘(’ token
printf("Leaving %s\n", (#fn));
^
e.c:11:26: error: stray ‘#’ in program
printf("Leaving %s\n", (#fn));
^
e.c:12:1: error: expected identifier or ‘(’ before ‘}’ token
} while(false)
^
e.c:12:3: error: expected identifier or ‘(’ before ‘while’
} while(false)
^~~~~
e.c: In function ‘main’:
e.c:30:1: error: expected ‘while’ at end of input
}
^
e.c:30:1: error: expected declaration or statement at end of input
*/
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2023 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.