summaryrefslogtreecommitdiffstats
path: root/md/writeup/c_macro_tricks.md
blob: e9f9d0ffacd3d67d83d9e0ea9f817a29ab65b690 (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
```c
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
```c
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

```c
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

```c
#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
```c
FUN(add,int a,int b);
FUN(mul,int a,int b,int c);
FUN(div,int a);
```

#### Result
```c
void add ( "there is 2");
void mul ( "there is 3");
void div ( "there is 1");
```

## C11 generics

### Match single argument

Best part of it that it can match also typdefed structures. So now macroses 
can contain typechecking

```c
#define type_str(T) _Generic( (T), int: "int",\
long: "long",\
A: "A",\
default: "Unknown type")
```

#### Source
```c
typedef struct A
{

} A;

#define type_str(T) _Generic( (T), int: "int",\
long: "long",\
A: "A",\
default: "Unknown type")

int main()
{
  A a;
  printf("%s\n",type_str((long)1));
  printf("%s\n",type_str((int)1));
  printf("%s\n",type_str(a));
}
```

#### Result

```block
long
int
A
```

### Match multiple arguments

```
#define type_str(T1,T2) _Generic( (T1),\
int:   _Generic((T2),int:"int int",   long: "int long",   float: "int float",   A: "int A",   default: "int UNK"),\
long:  _Generic((T2),int:"long int",  long: "long long",  float: "long float",  A: "long A",  default: "long UNK"),\
float: _Generic((T2),int:"float int", long: "float long", float: "float float", A: "float A", default: "float UNK"),\
A:     _Generic((T2),int:"A int",     long: "A long",     float: "A float",     A: "A A",     default: "A UNK"),\
default: "UNK UNK" )
```

#### Source
```c
typedef struct A
{

} A;

#define type_str(T1,T2) _Generic( (T1),\
int:   _Generic((T2),int:"int int",   long: "int long",   float: "int float",   A: "int A",   default: "int UNK"),\
long:  _Generic((T2),int:"long int",  long: "long long",  float: "long float",  A: "long A",  default: "long UNK"),\
float: _Generic((T2),int:"float int", long: "float long", float: "float float", A: "float A", default: "float UNK"),\
A:     _Generic((T2),int:"A int",     long: "A long",     float: "A float",     A: "A A",     default: "A UNK"),\
default: "UNK UNK" )

int main()
{
  A a;
  printf("%s\n",type_str((long)1,1.f));
  printf("%s\n",type_str((int)1,1));
  printf("%s\n",type_str(a,a));
}

```

#### Result

```
long float
int int
A A
```
### Generic printf
```c
#define FF "%f "
#define FS "%s "
#define FD "%d "
#define N ""
#define PR2(S,T,T1,T2,...) printf(S,T,T1)
#define PR1(S,T,T1,...) _Generic((T1),\
  int:  PR2(S FD,T,T1,__VA_ARGS__,N,N,N),\
  float:PR2(S FF,T,T1,__VA_ARGS__,N,N,N),\
  char*:PR2(S FS,T,T1,__VA_ARGS__,N,N,N),\
  default:"")
#define static_printf(T1,...) _Generic((T1),\ 
  int:  PR1(FD,T1,__VA_ARGS__,N,N,N),\
  float:PR1(FF,T1,__VA_ARGS__,N,N,N),\
  char*:PR1(FS,T1,__VA_ARGS__,N,N,N),\
  default:"")
```

#### Source
```c
int main()
{
  A a;
  int b = 3, c = 4;
  static_printf(1, 2.f);
  printf("\n");
  static_printf(b,c);
  printf("\n");

  static_printf("big float",0.01f);
  printf("\n");
}
```

#### Result

```bash
1 2.000000 
3 4 
big float  0.010000
```

## Links
1. [https://baike.baidu.com/item/C11](https://baike.baidu.com/item/C11)