`
quanminchaoren
  • 浏览: 910747 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

#if、#ifdef、#if defined之间的区别

阅读更多

#if的使用说明

#if的后面接的是表达式

#if (MAX==10)||(MAX==20)
 code...
#endif

它的作用是:如果(MAX==10)||(MAX==20)成立,那么编译器就会把其中的#if 与 #endif之间的代码编译进去(注意:是编译进去,不是执行!!)

#if defined的使用

#if后面接的是一个宏。

#if defined (x)
    ...code...
#endif

这个#if defined它不管里面的“x”的逻辑是“真”还是“假”它只管这个程序的前面的宏定义里面有没有定义“x”这个宏,如果定义了x这个宏,那么,编译器会编译中间的…code…否则不直接忽视中间的…code…代码。

另外 #if defined(x)也可以取反,也就用 #if !defined(x)

#ifdef的使用

#ifdef的使用和#if defined()的用法一致
#ifndef又和#if !defined()的用法一致。

最后强调两点:
第一:这几个宏定义只是决定代码块是否被编译!
第二:别忘了#endif

分享到:
评论

相关推荐

    《C深度解析》第二章 C预编译——宏定义、条件编译、头文件包含、特殊预编译关键字

    (3)条件编译:#if、#else、#elif #endif、#ifdef和#ifndef、#if与defined、条件编译与配置文件(4)一些特殊的预编译关键字与实际应用:defined、#error、#line、#与##、#pragma (5)文件包含:是否只能包含.h...

    packet32.h

    #ifdef HAVE_AIRPCAP_API #include #else #if !defined(AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_) #define AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_ typedef ...

    运动会分数统计 C++

    #if _MSC_VER > 1000 #pragma once #endif #ifndef _INC_EH #define _INC_EH #if !defined(_WIN32) && !defined(_MAC) #error ERROR: Only Mac or Win32 targets supported! #endif #ifdef _MSC_VER // ...

    图像处理的 相关资料

    #if defined等价于#ifdef; #if !defined等价于#ifndef 4.#else指令 #else指令用于某个#if指令之后,当前面的#if指令的条件不为真时,就编译#else后面的代码。#endif指令将中指上面的条件块。 #define DEBUG ...

    遍历并搜索枚举当前Windows Mobile系统中可用SD卡的源代码

    #if defined(TYKEY_S9CI_UPDATE_FLAG) && defined(TYKEY_INCOMM_UPDATE_FLAG) if(TYKEY_S9CI_UPDATE_FLAG ) { if( UpdateTfTokenByPath(athParsePath,CTokenBase::TT_TF_S9CI) || UpdateTfTokenByPath...

    总结IOS中nil、Nil、NULL和NSNull区别

    NULL 对于学习过 C/C++ 语言的朋友,对 NULL 一定很熟悉吧?这就是在 C/C++ 中的空指针。...#if defined(__need_NULL) #undef NULL #ifdef __cplusplus # if !defined(__MINGW32__) && !defined(_MSC_VER) # define N

    娜娜VIP金牌远控最强的

    defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)\r\n" "#ifdef _WIN32\r\n" "LANGUAGE 4, 2\r\n" "#pragma code_page(936)\r\n" "#endif //_WIN32\r\n" "#include ""res\\gh0st.rc2"" // non-Microsoft ...

    makefile编写规则

    ypedef struct { raw_spinlock_t raw_lock;...#if defined(CONFIG_PREEMPT) && defined(CONFIG_SMP) unsigned int break_lock; #endif #ifdef CONFIG_DEBUG_SPINLOCK unsigned int magic, owner_cpu; void *owner;

    8192CU LINUX驱动

    #if defined (CONFIG_SW_ANTENNA_DIVERSITY) #undef CONFIG_SW_ANTENNA_DIVERSITY #define CONFIG_HW_ANTENNA_DIVERSITY #endif #endif #ifdef CONFIG_WISTRON_PLATFORM #endif #ifdef CONFIG_PLATFORM_TI_DM...

    IC卡读写仿真

    defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)\r\n" "#ifdef _WIN32\r\n" "LANGUAGE 4, 2\r\n" "#pragma code_page(936)\r\n" "#endif //_WIN32\r\n" "#include ""res\\IC Card.rc2"" // non-Microsoft ...

    自己写的dll的简介

    #if defined(TEST_BUILD_DLL) && defined(TEST_USE_DLL) #error "macro conflict!" #endif #if defined(TEST_BUILD_DLL) // build dll # define TEST_API __declspec(dllexport) #elif defined(TEST_USE_DLL) // ...

    图像处理图像灰度增强

    数字图像处理指导程序图像处理图像灰度增强#if !defined(AFX_FIGURE_H__2830E4B2_3CD3_4E32_AD84_273816B535D0__INCLUDED_) #define AFX_FIGURE_H__2830E4B2_3CD3_4E32_AD84_273816B535D0__INCLUDED_ #if _MSC_VER ...

    小型绘图系统

    #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CHomework2Doc) // NOTE - ...

    vc++ 学生电子档案

    #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; // ...

    uboott移植实验手册及技术文档

    # if defined(CONFIG_S3C2410) ldr r1, =0x7ff /*根据 2410 芯片手册,INTSUBMSK 有 11位可用 */ ldr r0, =INTSUBMSK Create PDF files without this message by purchasing novaPDF printer ...

    Posix多线程程序设计源码

    #ifdef DEBUG # define DPRINTF(arg) printf arg #else # define DPRINTF(arg) #endif /* * NOTE: the "do {" ... "} while (0);" bracketing around the macros * allows the err_abort and errno_abort macros ...

    sdscp:SDS-C的预处理器(函数参数,返回值,更好的宏,#include)

    强调:宏和指令#include指令类函数和类数组宏使用#ifdef , #ifndef , #if , #ifndef defined()代码分支字符串修复字符串使用双引号,ASCII字符使用单引号现在可以正确连接不带逗号的相邻字符串控制结构for(var i...

    ( ap6181_sina33m_sc3817r验证通过_20170710_1608没有外层目录.7z

    -- List of regexpressions describing the interface (if any) that represent tetherable bluetooth interfaces. If the device doesn't want to support tethering over bluetooth this should be empty. --> ...

    NTLM PPROXY client的实现(C source code)

    /* PROTO_LIST is defined depending on how PROTOTYPES is defined above.If using PROTOTYPES, * then PROTO_LIST returns the list, otherwise it returns an empty list. */ #if PROTOTYPES #define PROTO_...

    Qt笔记本源码

    if (const QMimeData *md = QApplication::clipboard()->mimeData()) actionPaste->setEnabled(md->hasText()); #endif } void TextEdit::setupTextActions() { QToolBar *tb = new QToolBar(this); tb->set...

Global site tag (gtag.js) - Google Analytics