c++ 学习-预处理

2021/12/26 17:08:14

本文主要是介绍c++ 学习-预处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

预处理指令-完整自洽的语言

不属于C++语言,走的是预处理器

for what:

beyond c++ source scope?

#define

#undef

comes from Nginx and self use

1 #define ngx_tolower(c)      ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
2 #define ngx_toupper(c)      ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
3 
4 #define ngx_memzero(buf, n)       (void) memset(buf, 0, n
1 #define BEGIN_NAMESPACE(x)  namespace x {
2 #define END_NAMESPACE(x)    }
3 
4 BEGIN_NAMESPACE(my_own)
5 
6 ...      // functions and classes
7 
8 END_NAMESPACE(my_own)

 c++ 编译

 1 #ifdef __cplusplus                      // 定义了这个宏就是在用C++编译
 2     extern "C" {                        // 函数按照C的方式去处理
 3 #endif
 4     void a_c_function(int a);
 5 #ifdef __cplusplus                      // 检查是否是C++编译
 6     }                                   // extern "C" 结束
 7 #endif
 8 
 9 #if __cplusplus >= 201402                // 检查C++标准的版本号
10     cout << "c++14 or later" << endl;    // 201402就是C++14
11 #elif __cplusplus >= 201103              // 检查C++标准的版本号
12     cout << "c++11 or before" << endl;   // 201103是C++11
13 #else   // __cplusplus < 201103          // 199711是C++98
14 #   error "c++ is too old"               // 太低则预处理报错
15 #endif  // __cplusplus >= 201402         // 预处理语句结束

关于宏的一些使用操作

作者回复: 如果有好的IDE,它可以帮你找出宏的定义,加快理解。

还有一种办法,就是用课程里的“gcc -E”,让预处理器给你展开宏。

 



这篇关于c++ 学习-预处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程