-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacros.c
More file actions
30 lines (25 loc) · 808 Bytes
/
macros.c
File metadata and controls
30 lines (25 loc) · 808 Bytes
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 <errno.h>
#include <stdio.h>
#include <string.h>
#define ERR_MSG(fn) {(void)fflush(stderr); \
(void)fprintf(stderr, __FILE__ ":%d:" #fn ": %s\n", \
__LINE__, strerror(errno)); }
#define METAPRINTF(fn, args, exp) ({int ret = fn args; if(ret exp) ERR_MSG(fn); ret;})
#define PRINTF(args) METAPRINTF(printf, args, < 0)
#define FPRINTF(args) METAPRINTF(fprintf, args, < 0)
#define SCANF(args) METAPRINTF(scanf, args, < 0)
#define FSCANF(args) METAPRINTF(fscanf, args, < 0)
#define FFLUSH(args) METAPRINTF(fflush, args, < 0)
int main() {
int x;
PRINTF(("This is so cool!\n"));
FPRINTF((stderr, "This is on stderr!\n"));
PRINTF(("Enter a number: "));
int ret = SCANF(("%d", &x));
if (ret > 0) {
PRINTF(("%d\n", x));
} else {
printf("Invalid number!\n");
}
return 0;
}