-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacro.c
More file actions
42 lines (33 loc) · 1.1 KB
/
macro.c
File metadata and controls
42 lines (33 loc) · 1.1 KB
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
#include <stdio.h>
/* Normal Usage of multiple args */
#define PRINT(...) printf(__VA_ARGS__)
/* Given name to multiple args */
#define PRINT_ARGS(args...) printf(args)
/*
* This usage should give at least one arg after format,
* otherwise it becomes printf(format, ) which is wrong in GNU C,
* but seems allowed by GNU CPP in the following form.
*/
#define PRINT_WITH_ARG(format, ...) printf(format, __VA_ARGS__)
/* Normal usage of Sharpsharp sample */
#define PS(g, g2) g##g2
/*
* Special usage of Sharpsharp when placed between a comma and a variable
* argument. In this case, if the variable argument is left out, then the
* previous comma will be deleted too.
*/
#define PRINT_SHARPSHARP(format, ...) printf(format, ##__VA_ARGS__)
/* Pound sign: Transform name of arg to (const char*)"arg" */
#define PNAME_AND_VALUE(arg) printf(#arg " = %d\n", arg)
int
main()
{
int a = 10;
PRINT("%d\n", a);
PRINT_ARGS("%d\n", a);
//PRINT_WITH_ARG("ahhh\n"); // X
PRINT_SHARPSHARP("ahhh2\n"); // O
/* Usage sample */
PS(PRINT, _ARGS)("paste\n");
PNAME_AND_VALUE(a);
}