条件编译#define、#undef、#if、#elif、#elif defined、#elif !defined 、#endif用法

来源:互联网 发布:淘宝海淘被税了怎么办 编辑:程序博客网 时间:2024/06/11 23:27

条件编译

上代码

// what is the fucking shit.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <Windows.h>#define VERSION_XZM          1#define VERSION_HAERBIN      _T("哈尔滨\n")#define VERSION_TIANJIN      _T("天津\n")#define VERSION_SHAGNHAI     _T("上海\n")// //////////////////////////////////////////////////////////////////////////#define VERSION_01#define VERSION_02#define VERSION_03#define VERSION_04#define VERSION_05// undef//////////////////////////////////////////////////////////////////////////#undef VERSION_01#undef VERSION_02#undef VERSION_03#undef VERSION_04#undef VERSION_05int _tmain(int argc, _TCHAR* argv[]){int year = 2018 ;#if VERSION_XZM == 1OutputDebugString(_T("已定义--版本1\n"));#elif VERSION_XZM == 2OutputDebugString(_T("已定义--版本2\n"));#elif VERSION_XZM == 3OutputDebugString(_T("已定义--版本3\n"));#elif VERSION_XZM == 4OutputDebugString(_T("已定义--版本4\n"));#elif VERSION_XZM == 5OutputDebugString(_T("已定义--版本5\n"));#endif#ifdef VERSION_HAERBIN  // #if defined VERSION_HAERBINOutputDebugString(VERSION_HAERBIN);#elif defined VERSION_TIANJINOutputDebugString(VERSION_TIANJIN);#elif defined VERSION_SHAGNHAIOutputDebugString(VERSION_SHAGNHAI);#endif#ifndef VERSION_HAERBINOutputDebugString(VERSION_HAERBIN);#elif defined VERSION_TIANJINOutputDebugString(VERSION_TIANJIN);#elif defined VERSION_SHAGNHAIOutputDebugString(VERSION_SHAGNHAI);#endif#if defined  VERSION_01OutputDebugString(_T("已定义--版本01\n"));#elif defined VERSION_02OutputDebugString(_T("已定义--版本02\n"));#elif (!defined VERSION_03) || (!defined VERSION_04)OutputDebugString(_T("未定义--版本03和版本04\n"));#elif !defined VERSION_05OutputDebugString(_T("未定义--版本05\n"));#endifreturn 0;}
上图


1.#ifdefVERSION_HAERBIN 和 #if definedVERSION_HAERBIN的功能是一样的


2.#ifdef VERSION_HAERBIN如果定义了VERSION_HAERBIN宏,则


3.#ifVERSION_XZM == 1 如果VERSION_XZM 的值等于1则执行


4.#elif defined VERSION_TIANJIN否则,如果定义了VERSION_TIANJIN宏,则


5.#elif (!definedVERSION_03) || (!definedVERSION_04)否则,如果即没定义VERSION_03也没定义VERSION_04,则


6.#endif结束条件编译


7.如果条件刚开始成立了,就直接结束了

#if 
#elif 
#elif 
#endif

8.忘记了#else了,哈哈


接下来应该记录一些常用的宏定义才是,下篇再说

阅读全文
0 0