大型程序或者修改别人的程序时,当我们需要定义常量(源文件还是头文件 ),我们就必须返回检查原来此常量是否已经定义,

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用法的更多相关文章

  1. C++ #if #endif #define #ifdef #ifndef #if defined #if !defined详解 (转)

    (源)http://blog.csdn.net/sky1203850702/article/details/42024673 首先,让我们先从头文件开始,在很多头文件里,我们会看到这样的语句 #ifn ...

  2. #ifdef 和 #if defined 的区别 -- 转

    #ifdef 和 #if defined 的区别在于,后者可以组成复杂的预编译条件,比如 #if defined (AAA) && defined (BBB) xxxxxxxxx #e ...

  3. #ifdef和#if defined的差别

    注意两者都有个define的作用,区别在于使用方式上.前者的通常用法是:#ifdef  XXX .... #else .... #endif 只能在两者中选择是否有定义.对于后者,常用法是: #if ...

  4. #ifdef 和 #if defined的区别

    #ifdef 和 #if defined的区别在于,后者可以组成复杂的预编译条件,比如 #if defined (AAA) && defined (BBB)xxxxxxxxx#endi ...

  5. (五)c语言条件编译#ifdef与#if defined

    c语言条件编译#ifdef与#if defined defined NAME是用来判断NAME是否被定义了(被用define定义了). #ifdef NAME == #if defined(NAME) ...

  6. #if defined 的意思?

    在读s3c2440a的test程序,其中option.h文件中有段语句为: #define LCD_N35 //#define LCD_L80 //#define LCD_T35 //#define ...

  7. 有了#ifdef 为什么还需要#if defined

    有了#ifdef 为什么还需要#if  defined ? #include <stdio.h> #define A #define B void test(int a,int b) { ...

  8. c语言条件编译#ifdef与#if defined

    c语言条件编译#ifdef与#if defined   c语言条件编译#ifdef与#if defined 摘自:https://www.cnblogs.com/zhangshenghui/p/566 ...

  9. Unity5 GI与PBS渲染从用法到着色代码

    http://www.cnblogs.com/zhouxin/p/5168632.html 本文主要介绍Untiy5以后的GI,PBS,以及光源探头,反射探头的用法以及在着色器代码中如何发挥作用,GI ...

随机推荐

  1. Nginx的 HTTP 499 状态码处理

    1.前言 今天在处理一个客户问题,遇到Nginx access log中出现大量的499状态码.实际场景是:客户的域名通过cname解析到我们的Nginx反向代理集群上来,客户的Web服务是由一个负载 ...

  2. ArcGIS API for JavaScript 4.2学习笔记[22] 使用【QueryTask类】进行空间查询 / 弹窗样式

    上一篇写道,使用Query类进行查询featureLayer图层的要素,也简单介绍了QueryTask类的使用. 这一篇博文继续推进,使用Query类和QueryTask类进行空间查询,查询USA的著 ...

  3. CDQ分治与整体二分小结

    前言 这是一波强行总结. 下面是一波瞎比比. 这几天做了几道CDQ/整体二分,感觉自己做题速度好慢啊. 很多很显然的东西都看不出来 分治分不出来 打不出来 调不对 上午下午晚上的效率完全不一样啊. 完 ...

  4. 重写JS的鼠标右键点击菜单

    重写JS的鼠标右键点击菜单 该效果主要有三点,一是对重写的下拉菜单的隐藏和显示:二是屏蔽默认的鼠标右键事件:三是鼠标左键点击页面下拉菜单隐藏. 不多说,上html代码: 1 <ul id=&qu ...

  5. 4、树莓派的中文:安装ftp,安装gcc,安装qt,编译qt程序,运行qt界面程序

    本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.安装FTP:sudo apt-get install vsftpd 2.配置FTP,修改,/etc/vsftpd.conf # ...

  6. QT中几个函数的使用方法

    一.把字符串转换成整形demo1:QString str = "FF";bool ok;int hex = str.toInt(&ok, 16); // hex == 25 ...

  7. centos7 编译ntopng源码

    先安装编译所需的开发工具 yum groupinstall 'Development Tools' yum install tcl yum install libpcap libpcap-devel ...

  8. MVC框架实例构建

    转自:http://www.cnblogs.com/levenyes/p/3290885.html MVC全名是Model View Controller,是模型(model)-视图(view)-控制 ...

  9. 如何在开发时部署和运行前后端分离的JavaWeb项目

    在开发中大型的JavaEE项目时,前后端分离的框架逐渐成为业界的主流,传统的单机部署前后端在同一个项目中的工程项目越来越少.这类JavaWeb项目的后端通常都采用微服务的架构,后端会被分解为诸多个小项 ...

  10. QQ音乐API-借他人之力实现我的音乐盒

    好久没有写博客了,最近升级做爸爸了,很开心的事情.内心又很忧郁,怎么能给媳妇和儿子一个相对好的物质经济条件.现在什么都没有的我,至少还有你们. 话不多说了,这篇博客还是和自己用vue做web app相 ...