2013-10-14 02:36:58 +00:00
|
|
|
---
|
|
|
|
title: C Preprocessor
|
2018-12-06 22:15:40 +00:00
|
|
|
category: C-like
|
2020-07-05 13:13:06 +00:00
|
|
|
intro: |
|
|
|
|
Quick reference for the [C macro preprocessor](https://en.m.wikipedia.org/wiki/C_preprocessor), which can be used independent of C/C++.
|
2013-10-14 02:36:58 +00:00
|
|
|
---
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
## Reference
|
|
|
|
{: .-three-column}
|
|
|
|
|
2013-10-14 01:55:21 +00:00
|
|
|
### Compiling
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
$ cpp -P file > outfile
|
|
|
|
```
|
2013-10-14 01:55:21 +00:00
|
|
|
|
|
|
|
### Includes
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
#include "file"
|
|
|
|
```
|
2013-10-14 01:55:21 +00:00
|
|
|
|
|
|
|
### Defines
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
#define FOO
|
|
|
|
#define FOO "hello"
|
2013-10-14 01:55:21 +00:00
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
#undef FOO
|
|
|
|
```
|
2013-10-14 01:55:21 +00:00
|
|
|
|
|
|
|
### If
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
#ifdef DEBUG
|
|
|
|
console.log('hi');
|
|
|
|
#elif defined VERBOSE
|
|
|
|
...
|
|
|
|
#else
|
|
|
|
...
|
|
|
|
#endif
|
|
|
|
```
|
2013-10-14 01:55:21 +00:00
|
|
|
|
|
|
|
### Error
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
#if VERSION == 2.0
|
|
|
|
#error Unsupported
|
|
|
|
#warning Not really supported
|
|
|
|
#endif
|
|
|
|
```
|
2013-10-14 01:55:21 +00:00
|
|
|
|
|
|
|
### Macro
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
#define DEG(x) ((x) * 57.29)
|
|
|
|
```
|
2013-10-14 01:55:21 +00:00
|
|
|
|
|
|
|
### Token concat
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
#define DST(name) name##_s name##_t
|
2021-02-13 01:05:44 +00:00
|
|
|
DST(object); #=> object_s object_t;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Stringification
|
|
|
|
|
|
|
|
```
|
|
|
|
#define STR(name) #name
|
|
|
|
char * a = STR(object); #=> char * a = "object";
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
2013-10-14 01:55:21 +00:00
|
|
|
|
|
|
|
### file and line
|
|
|
|
|
2020-07-05 13:13:06 +00:00
|
|
|
```
|
|
|
|
#define LOG(msg) console.log(__FILE__, __LINE__, msg)
|
|
|
|
#=> console.log("file.txt", 3, "hey")
|
|
|
|
```
|