#ifdef and #ifndef

Preprocessor: #ifdef and #ifndef

The #ifdef (if defined) and #ifndef (if not defined) preprocessor commands are used to test if a preprocessor variable has been "defined". There are two common uses for this, with slightly different patterns.

Prevent multiple definitions in header files

When there definitions in a header file that can not be made twice, the code below should be used. A header file may be included twice other include files include it, or an included file includes it and the source file includes it again.

To prevent bad effects from a double include, it is common to surround the body in the include file with the following (where MYHEADER_H is replaced by a name that is appropriate for your program).

#ifndef MYHEADER_H
#define MYHEADER_H
. . .	// This will be seen by the compiler only once 
#endif /* MYHEADER_H */

Turning debugging code off and on

Debugging code is necessary in programs, however, it is not usually appropriate to leave it in the delivered code. The preprocessor #ifdef commmand can surround the debugging code. If DEBUG is defined as below (probably in an include file) all debugging statement surrounded by the #ifdef DEBUG statement will be active. However, if it isn't defined, none of the statements will make it thru the preprocessor.

#define DEBUG
. . .
#ifdef DEBUG
  . . . // debugging output
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值