#if, #ifdef, #ifndef, #else, #elif, #endif的用法
来源:互联网 发布:环保部网络举报平台 编辑:程序博客网 时间:2024/05/16 12:36
#elif是预处理指令,跟#if等一起用,进行条件编译,比较完整的格式应该是:
#elif 条件 表达式;
………
#else 条件 表达式;
#if 条件 表达式;
#endif;
条件和表达式语句间用空格分开
elif相当于else if,判断中间可以弄n个elif
条件判断完最后一定要加#endif哦!
这些命令可以让编译器进行简单的逻辑控制,当一个文件被编译时,你可以用这些命令去决定某些代码的去留,
这些命令式条件编译的命令。
常见的条件编译的三种形式:
①第一种形式:
#if defined(或者是ifdef)<标识符(条件)>
<程序段1>
#endif
②第二种形式:
#if !defined(或者是ifndef)<标识符(条件)>
<程序段1>
#ifdef …
[#elif … ]
[#elif …]
#else …
#endif
示例:
#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
- #if, #ifdef, #ifndef, #else, #elif, #endif的用法
- #if, #ifdef, #ifndef, #else, #elif, #endif的用法
- #if,#ifdef,#ifndef, #elif, #else, #endif 使用
- #ifdef #else #endif #if #ifndef 的用法
- #if, #if defined, #ifdef, #ifndef, #elif, #else, #endif 使用
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- #ifdef #else #endif #if #ifndef
- #ifdef(#ifndef), #if, #else, #endif
- #ifdef,#else,#endif和#ifndef的用法
- 记录RecyclerView滚动位置并恢复
- 制造企业鼎新WORKFLOW ERP拓展之车间制程管理系统
- shell脚本实现ssh自动登录-功能扩展,实现多个IP可选
- leetcode15 3Sum(java 实现)
- ARP协议详解 ARP报文结构
- #if, #ifdef, #ifndef, #else, #elif, #endif的用法
- #pragma omp threadprivate(alpha)
- ffmpeg常用命令
- VL09碰到了傻傻的NOTE了
- Android学习资源-retrofit,eventBus,butterknife,fragment
- 简单sql题
- oracle更新时间
- 四川
- [ORACLE]Centos7.2安装ASM遇到的问题