cheatsheets/c_preprocessor.md

79 lines
952 B
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
title: C Preprocessor
2018-12-06 22:15:40 +00:00
category: C-like
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
---
## Reference
{: .-three-column}
2013-10-14 01:55:21 +00:00
### Compiling
```
$ cpp -P file > outfile
```
2013-10-14 01:55:21 +00:00
### Includes
```
#include "file"
```
2013-10-14 01:55:21 +00:00
### Defines
```
#define FOO
#define FOO "hello"
2013-10-14 01:55:21 +00:00
#undef FOO
```
2013-10-14 01:55:21 +00:00
### If
```
#ifdef DEBUG
console.log('hi');
#elif defined VERBOSE
...
#else
...
#endif
```
2013-10-14 01:55:21 +00:00
### Error
```
#if VERSION == 2.0
#error Unsupported
#warning Not really supported
#endif
```
2013-10-14 01:55:21 +00:00
### Macro
```
#define DEG(x) ((x) * 57.29)
```
2013-10-14 01:55:21 +00:00
### Token concat
```
#define DST(name) name##_s name##_t
DST(object); #=> object_s object_t;
```
### Stringification
```
#define STR(name) #name
char * a = STR(object); #=> char * a = "object";
```
2013-10-14 01:55:21 +00:00
### file and line
```
#define LOG(msg) console.log(__FILE__, __LINE__, msg)
#=> console.log("file.txt", 3, "hey")
```