当前位置:网站首页>预编译处理指令中的条件编译
预编译处理指令中的条件编译
2022-06-26 18:07:00 【Maccy37】
#if,#ifdef,#ifndef,#else,#elif,#endif的用法:预编译处理语句都是以#开头,其结尾不带分号,与普通程序语句相区别。这些命令可以让编译器进行简单的逻辑控制,当一个文件被编译时,可以用这些命令去决定某些代码的去留,这些是命令式条件编译的命令。
常见的条件编译的三种形式:
①第一种形式:
#if defined(或者是ifdef)<标识符(条件)>
<程序段1>
[#else
<程序段2>]
#endif
②第二种形式:
#if !defined(或者是ifndef)<标识符(条件)>
<程序段1>
[#else
<程序段2>]
#endif
③第三种形式:
#ifdef …
[#elif … ]
[#elif …]
#else …
#endif
类似于:
if(<条件>)
<语句>
else if(<条件>)
<语句>
else if(<条件>)
<语句>
.............
else
<语句>示例:
#include <iostream>
using namespace std;
int main()
{
#if DEBUG /*或者是#ifdef DEBUG*/
cout << "条件成立,DEBUG已经定义了!" <<endl;
#else
cout << "条件不成立,DEBUG还没定义" <<endl;
#endif
return 0;
}
//结果输出:条件不成立,DEBUG还没定义
//如果是添加了#define DEBUG ,输出结果是:条件成立,DEBUG已经定义了!
#include <iostream>
using namespace std;
#define DEBUG
int main()
{
#ifdef DEBUG /*或者是#ifdef DEBUG*/
cout << "条件成立,DEBUG已经定义了!" <<endl;
#else
cout << "条件不成立,DEBUG还没定义" <<endl;
#endif
return 0;
}
//要注意的是,如果是#define 宏名,没有宏体如 #define DEBUG,就必须使用#ifdef或#ifndef与之对应,
//如果是#define 宏名 宏体,如 #define NUM 1,#if 和#ifdef都可以使用。
/*
#define的用法:
*/
示例二:
#include <iostream>
using namespace std;
#define NUM 10
int main()
{
#ifndef NUM
cout << "NUM没有定义!"<<endl;
#elif NUM >= 100
cout << "NUM >100" <<endl;
#elif NUM <100 && NUM >10
cout << "10 < NUM < 100" <<endl;
#elif NUM == 10
cout << "NUM ==10" <<endl;
#else
cout << "NUM < 10" << endl;
#endif
return 0;
}
//输出NUM ==10
边栏推荐
猜你喜欢

Dos et détails de la méthode d'attaque
![[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure](/img/26/da00e70d0955bcdef3362474bc5fc7.png)
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure

#26class中get和set设置

Analysis of deep security definition and encryption technology

Solve the problem that each letter occupies a space in pycharm

Rich professional product lines, and Jiangling Ford Lingrui · Jijing version is listed

小程序设置按钮分享功能

接水面试题

wechat_微信小程序中解决navigator进行页面跳转并传递参数问题

MySQL add column failed because there was data before, not null by default
随机推荐
RSA encryption and decryption details
【NPOI】C#跨工作薄复制Sheet模板导出Excel
A little experience of next (ITER (dataloader))
Leetcode - 226. Retourner l'arbre binaire (bfs)
pycharm如何修改多行注释快捷键
Concept and working principle of data encryption standard (DES)
在国金证券开户怎么样?开户安全吗?
Case study of row lock and isolation level
Map and list < map > transfer to corresponding objects
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!
[code Capriccio - dynamic planning] t583. Deleting two strings
RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
Regular match same character
In and exceptions, count (*) query optimization
数据加密标准DES安全性
js强制转换
Properties file garbled
Paging query and join Association query optimization
next(iter(dataloader))的一点点体会
[unity] use C in unity to execute external files, such as Exe or bat