OSDN Git Service

Regular updates
[twpd/master.git] / c_preprocessor.md
1 ---
2 title: C Preprocessor
3 category: C-like
4 layout: 2017/sheet
5 intro: |
6   Quick reference for the [C macro preprocessor](https://en.m.wikipedia.org/wiki/C_preprocessor), which can be used independent of C/C++.
7 ---
8
9 ## Reference
10 {: .-three-column}
11
12 ### Compiling
13
14 ```
15 $ cpp -P file > outfile
16 ```
17
18 ### Includes
19
20 ```
21 #include "file"
22 ```
23
24 ### Defines
25
26 ```
27 #define FOO
28 #define FOO "hello"
29
30 #undef FOO
31 ```
32
33 ### If
34
35 ```
36 #ifdef DEBUG
37   console.log('hi');
38 #elif defined VERBOSE
39   ...
40 #else
41   ...
42 #endif
43 ```
44
45 ### Error
46
47 ```
48 #if VERSION == 2.0
49   #error Unsupported
50   #warning Not really supported
51 #endif
52 ```
53
54 ### Macro
55
56 ```
57 #define DEG(x) ((x) * 57.29)
58 ```
59
60 ### Token concat
61
62 ```
63 #define DST(name) name##_s name##_t
64 DST(object);   #=> object_s object_t;
65 ```
66
67 ### Stringification
68
69 ```
70 #define STR(name) #name
71 char * a = STR(object);   #=> char * a = "object";
72 ```
73
74 ### file and line
75
76 ```
77 #define LOG(msg) console.log(__FILE__, __LINE__, msg)
78 #=> console.log("file.txt", 3, "hey")
79 ```