#if defined、#if !defined用法
大型程序或者修改别人的程序时,当我们需要定义常量(源文件还是头文件 ),我们就必须返回检查原来此常量是否已经定义,
if defined宏 就是用于检测的。
举个例子,如下:
#define ....
#define .... .... ....
#define Dataauto 1 ....
要检查Dataauto是否定义,或者我们要给Dataauto一个不同的值,可以添加语句
#if defined a
#undef Dataauto
#define Dataauto 0
#endif 上述语句检验Dataauto是否被定义,如果被定义,则用#undef语句解除定义,并重新定义Dataauto为0
同样,检验Dataauto是否定义:
#ifndef Dataauto //如果Dataauto没有被定义
#define Dataauto 0
#endif
以上所用的宏中:#undef为解除定义,#ifndef是if not defined的缩写,即如果没有定义。
这就是#if defined 的唯一作用!
1) #if defined XXX_XXX
#endif 是条件编译,是根据你是否定义了XXX_XXX这个宏,而使用不同的代码。
一般.h文件里最外层的
#if !defined XXX_XXX
#define XXX_XXX
#endif 是为了避免.h头文件被重复include。
2) #error XXXX 是用来产生编译时错误信息XXXX的,一般用在预处理过程中;
例子:
#if !defined(__Dataauto)
#error C++ compiler required.
#endif
引用别人的英文用法说明:
The special operator defined is used in #if and #elif expressions to test whether a certain name is defined as a macro.
defined name and defined (name) are both expressions whose value is 1 if name is defined as a macro at the current point in the program, and 0 otherwise.
Thus, #if defined MACRO is precisely equivalent to #ifdef MACRO. defined is useful when you wish to test more than one macro for existence at once.
For example, #if defined (__vax__) || defined (__ns16000__) would succeed if either of the names __vax__ or __ns16000__ is defined as a macro. Conditionals written like this: #if defined BUFSIZE && BUFSIZE >= 1024 can generally be simplified to just #if BUFSIZE >= 1024, since if BUFSIZE is not defined, it will be interpreted as having the value zero.
If the defined operator appears as a result of a macro expansion, the C standard says the behavior is undefined. GNU cpp treats it as a genuine defined operator and evaluates it normally. It will warn wherever your code uses this feature if you use the command-line option -pedantic, since other compilers may handle it differently .
#if defined、#if !defined用法的更多相关文章
- C++ #if #endif #define #ifdef #ifndef #if defined #if !defined详解 (转)
(源)http://blog.csdn.net/sky1203850702/article/details/42024673 首先,让我们先从头文件开始,在很多头文件里,我们会看到这样的语句 #ifn ...
- #ifdef 和 #if defined 的区别 -- 转
#ifdef 和 #if defined 的区别在于,后者可以组成复杂的预编译条件,比如 #if defined (AAA) && defined (BBB) xxxxxxxxx #e ...
- #ifdef和#if defined的差别
注意两者都有个define的作用,区别在于使用方式上.前者的通常用法是:#ifdef XXX .... #else .... #endif 只能在两者中选择是否有定义.对于后者,常用法是: #if ...
- #ifdef 和 #if defined的区别
#ifdef 和 #if defined的区别在于,后者可以组成复杂的预编译条件,比如 #if defined (AAA) && defined (BBB)xxxxxxxxx#endi ...
- (五)c语言条件编译#ifdef与#if defined
c语言条件编译#ifdef与#if defined defined NAME是用来判断NAME是否被定义了(被用define定义了). #ifdef NAME == #if defined(NAME) ...
- #if defined 的意思?
在读s3c2440a的test程序,其中option.h文件中有段语句为: #define LCD_N35 //#define LCD_L80 //#define LCD_T35 //#define ...
- 有了#ifdef 为什么还需要#if defined
有了#ifdef 为什么还需要#if defined ? #include <stdio.h> #define A #define B void test(int a,int b) { ...
- c语言条件编译#ifdef与#if defined
c语言条件编译#ifdef与#if defined c语言条件编译#ifdef与#if defined 摘自:https://www.cnblogs.com/zhangshenghui/p/566 ...
- Unity5 GI与PBS渲染从用法到着色代码
http://www.cnblogs.com/zhouxin/p/5168632.html 本文主要介绍Untiy5以后的GI,PBS,以及光源探头,反射探头的用法以及在着色器代码中如何发挥作用,GI ...
随机推荐
- 如何在markdown中插入表情
Markdown是很好用的呀,个人灰常的喜欢,也是灰常漂亮的,但是如何在写文章的时候插入表情呢,下面给出一部分代码
- Windows编程之进程遍历(C++实现)
Windows编程之进程遍历 PS: 主要扣代码使用,直接滑动到最下面使用. 遍历进程需要几个API,和一个结构体 1.创建进程快照 2.遍历首次进程 3.继续下次遍历 4.进程信息结构体 API 分 ...
- MySQL sql语句获取当前日期|时间|时间戳
1.1 获得当前日期+时间(date + time)函数:now() mysql> select now();+———————+| now() |+———————+| 2013-04-08 20 ...
- python 动态加载类对象
第一步 加载模块 module =__import__("modulename",fromlist=['']) 第二部 加载类对象 cls = getattr(module, & ...
- 回顾2017系列篇(二):移动端APP设计趋势
移动端APP在2017年经历了诸多的变化, 人工智能.聊天式的界面.响应式设计.虚拟现实(VR)和增强现实(AR)让设计师不断面临新的挑战.研究表明,用户每天耗费在手机和平板上的平均时长为158分钟, ...
- 转载|chrome developer tool—— 断点调试篇
断点,调试器的功能之一,可以让程序中断在需要的地方,从而方便其分析.也可以在一次调试中设置断点,下一次只需让程序自动运行到设置断点位置,便可在上次设置断点的位置中断下来,极大的方便了操作,同时节省了时 ...
- canvas学习api
1.canvas.getContext():获取渲染上下文和绘画功能: 一.绘制矩形 2.ctx.fillRect(x,y,width,height):绘制矩形: 3.ctx.strokeRect(x ...
- 手撕vue-cli配置文件——config篇
最近一直在研究webpack,突然想看看vue-cli中的webpack是如何配置,查阅了很多相关的文章,所以也想出几篇关于vue-cli配置的东西.正所谓"工欲善其事必先利其器" ...
- Linux(CentOS6.5)下修改Nginx初始化配置
本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 首先备份相关文件: cp /comexHome/nginx/conf ...
- Centos6.8防火墙配置
1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service iptables star ...