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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
title: C macro tricks
keywords:c,macro,c11,generics
# C macro tricks
## GCC preprocess macros
If you need to run just macro preprocessor without running compiler use
```bash
gcc -E source.c
```
this allows to see resulting source that going to be compiled, macro errors
could be hard to debug, but this is first thing, test them before and then
be sure that everything works. Lets continue with some more deep stuff.
<!-- ####################################################################### -->
## __VA_ARGS__ keyword
<!-- ####################################################################### -->
### Single argument macros
Writting macros with single argument
```c
#define F(X) X
```
#### Source
So lets code just with one macro
```c
#define F(X) X
F(int main)
F((){)
F(printf("hello world\n");)
F(})
```
any kind of argument can be passed to macro, and that allows to make some tricks
#### Result
```
int main
(){
printf("hello world\n");
}
```
<!-- ####################################################################### -->
### Multi argument macro
writting macro with multiple unamed arguments
```c
#define F(...) __VA_ARGS__
```
#### Source
```c
#define F(...) __VA_ARGS__
F(int main)
F((){)
F(printf("hello world\n");)
F(})
F(1,2,3,4,5)
```
Previouse example works just fine, but if add multiple arguments the __VA_ARGS__
just prints them as a whole string
#### Result
```
int main
(){
printf("hello world\n");
}
1,2,3,4,5
```
<!-- ####################################################################### -->
### Mixing named arguments and unamed arguments
Mixing together named and unnamed arguments
```c
#define F(X,...) X __VA_ARGS
```
#### Source
```c
#define F1(X,...) __VA_ARGS__
#define F2(X,...) X
F1(1,2,3,4,5)
F2(1,2,3,4,5)
F1(int main,{my code},{more code})
F2(int main,{my code},{more code})
```
#### Result
```
2,3,4,5
1
{my code},{more code}
int main
```
<!-- ####################################################################### -->
## Define struct with macros
Lets move to some more practical example lets just define macro that going to
create proper C structure.
C structure have this kind of syntax
```block
struct <name-struct>
{
<type-name> <variable-name>;
<type-name> <variable-name>;
...
...
};
```
```c
#define N
#define M2(X1,X2,...) X1 X2;
#define M1(X1,X2,...) X1 X2; M2(__VA_ARGS__,N,N)
#define M(X,...) struct X {M1(__VA_ARGS__,N,N)};
```
Here we pass variable arguments first macro M preprocess first arguments and
pass all left-over aruments with __VA_ARGS__ to M1, M1 preprocess 2 arguments
(2nd,3rd) and pass left-over arguments to next M2 macro.
There is small trict to make support any number of arguments, there is defined
macro N that is just empty macro, so macro M support any number of arguments
and if there is not define needed amount of arguments N is passed, and if there
is not enought arguments N is used.
<!--
Correct amount of arguments is 1,3,5. If need to define more then need to
define deeper structure of M arguments. Dont forget that macro maximum argument
number is 64 so, with this structure you can define just 31 field of structure.
There could be done small fix and it could support 63 arguments.
-->
#### Source
```c
#define N
#define M2(X1,X2,...) X1 X2;
#define M1(X1,X2,...) X1 X2; M2(__VA_ARGS__,N,N)
#define M(X,...) struct X {M1(__VA_ARGS__,N,N)};
M(add,int,a,int,b);
M(dirst,int,c);
```
#### Result
```
struct add {int a; int b;};;
struct dirst {int c; ;};;
```
<!-- ####################################################################### -->
## Detect number of arguments
There is one trick that can be used to detect number of arguments passed to
macro. This common example found in internet. There could be made some
improvment or can be added some trickery. But for now most important feature
is that it works.
```c
#define PP_NARG(...) \
PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
#define PP_NARG_(...) \
PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N( \
_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, N, ...) N
#define PP_RSEQ_N() \
63,62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
```
#### Source
```c
#define F(...) PP_NARG(__VA_ARGS__)
F(0)
F()
F(1,2,3,4,5)
```
#### Result
```
1
1
5
```
<!-- ####################################################################### -->
## Variable argument macro match macro according number of arguments
Detect number of arguments and match macro according to number of arguments
```
#define FUN3(X1,X2,X3,...) "there is 3"
#define FUN2(X1,X2,...) "there is 2"
#define FUN1(X1,...) "there is 1"
#define FUNN(X,A) X ## A
#define FUNN1(X,A) FUNN(X,A)
#define FUN(X,...) void X ( FUNN1(FUN,PP_NARG(__VA_ARGS__))(__VA_ARGS__))
```
#### Source
```
FUN(add,int a,int b);
FUN(mul,int a,int b,int c);
FUN(div,int a);
```
#### Result
```
void add ( "there is 2");
void mul ( "there is 3");
void div ( "there is 1");
```
|