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

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. Java之路第一步——第一行Java代码

    main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法. 名字必须是main: 必须是public static void 类型的: 必须接收一 ...

  2. 关于foo的一个面试题

    今天看到一个关于foo的一个面试题,赶脚特别有意思 function foo(){// 第16行 getName = function(){console.log(1)} return this } ...

  3. 获取数据库时间sql 以及行级锁总结-共享锁-排他锁-死锁

    --TRUNC(date,[fmt]) /TRUNC(number[,decimals])SELECT SYSDATE FROM dual;SELECT TRUNC(SYSDATE) FROM dua ...

  4. INITTAB 配置文件

    Inittab 文件详解       init的进程号是1(ps -aux | less),从这一点就能看出,init进程是系统所有进程的起点,Linux在完成核内引导以后,就开始运行init程序. ...

  5. Python函数篇(7)-正则表达式

    1.正则表达式   正则表达式为高级的文本模式匹配,抽取,与/或文本形式的搜索和替换功能提供了基础,简单的来说,正则表达式是由一些字符和特殊符号组成的字符串.Python通过标准库中的re模块来支持正 ...

  6. php一篇入门

    <?php header("Content-type: text/html; charset=utf-8");//设置编码也可以通过html中的 head中的 <met ...

  7. [js高手之路] es6系列教程 - 迭代器,生成器,for...of,entries,values,keys等详解

    接着上文[js高手之路] es6系列教程 - 迭代器与生成器详解继续. 在es6中引入了一个新的循环结构for ....of, 主要是用来循环可迭代的对象,那么什么是可迭代的对象呢? 可迭代的对象一般 ...

  8. 阿里maven仓库地址 和 oschina maven仓库地址

    <mirror>     <id>nexus-aliyun</id>     <mirrorOf>*</mirrorOf>     < ...

  9. .NET Core+MySql+Nginx 容器化部署

    .NET Core容器化@Docker .NET Core容器化之多容器应用部署@Docker-Compose .NET Core+MySql+Nginx 容器化部署 GitHub-Demo:Dock ...

  10. Python2和Python3中的字符串编码问题解决

    Python2和Python3在字符串编码上是有明显的区别. 在Python2中,字符串无法完全地支持国际字符集和Unicode编码.为了解决这种限制,Python2对Unicode数据使用了单独的字 ...