预处理三剑客(宏定义,文件包含和条件编译)之第三种——条件编译。 #ifndef #ifdef

预处理三剑客(宏定义,文件包含和条件编译)之第三种——条件编译。 #ifndef #ifdef

用法

    #ifdef  标识符                          #ifndef  标识符

    程序段1                                       程序段1

    #else                                         #else

    程序段2                                       程序段2

    #endif                                      #endif
项目内容
目的1 头文件使用,防止重复调用该头文件
-2 测试使用,省去注释的麻烦(定义了就不再定义)
-3 不同场景的选择(如果定义了是a场景,如果没有就是b场景,比如求面积,边长l矩形; 圆形半径r
制定程序|-main.cpp
-|-pracifdef.cpp
-|-pracifdef.h

Main

/*
 * main.cpp
 *
 *  Created on: 2021年5月19日
 *      Author: c00525771
 */
 
#include "pracifdef.h"
 
#include <iostream>
#define HANDL_
//#define ADD_
 
using namespace std;
 
int main() {
#ifdef HANDL_ //here for (2) test selection
cout << "(2) Hello World, I used ifdef cmds!!!" << endl; // prints !!!Hello World!!!
#endif
pracifdef();
 
int a = 10, b = 100, c = 0;
#ifdef ADD_
c = a + b;
cout << "(3) ifdef ADD_: c= " << c << endl;
#else
c = a - b;
cout << "(3) ifndef ADD_: c= " << c << endl;
#endif
 
return 0;
}

Pracifdef.cpp



 * pracifdef.cpp
 *
 *  Created on: 2021519*      Author: c00525771
 */
#include "pracifdef.h"
#include <iostream>
 
using namespace std;
 
pracifdef::pracifdef() {
// TODO Auto-generated constructor stub
   std::cout <<"I am another program"<< std::endl;
}
 
pracifdef::~pracifdef() {
// TODO Auto-generated destructor stub
}
 

Pracifdef.h



/*
 * pracifdef.h
 *
 *  Created on: 2021年5月19日
 *      Author: c00525771
 */
 
#ifndef PRACIFDEF_H_ //here for (1) avoid repeat call
#define PRACIFDEF_H_
 
class pracifdef {
public:
pracifdef();
virtual ~pracifdef();
};
 
#endif /* PRACIFDEF_H_ */

测试

打开场景:

(2) Hello World, I used ifdef cmds!!!
I am another program
(3) ifdef ADD_: c= 110

关闭场景: //#define ADD_

(2) Hello World, I used ifdef cmds!!!
I am another program
(3) ifndef ADD_: c= -90

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值