谁知道#ifndef 和#if !define 的区别?

wangweicai 2002-12-29 01:22:17
谁知道#ifndef 和#if !define 的区别?
如果没有区别的话,应该怎么用?
...全文
1345 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaohangcom 2003-03-01
  • 打赏
  • 举报
回复
`
hudgan 2002-12-29
  • 打赏
  • 举报
回复
study
wangweicai 2002-12-29
  • 打赏
  • 举报
回复
riverboat(诸葛不亮) :
对不起,
谢了。
riverboat 2002-12-29
  • 打赏
  • 举报
回复
BTW,你的defined写掉了一个d
:)
riverboat 2002-12-29
  • 打赏
  • 举报
回复
基本上一样,不过后者的应用范围更大,可以支持多个预编译变量的检查:
#if (!defined(_DEBUG) && defined(USE_MYLIB))
..........
#endif

这种情况用前一种方式就只能写一个嵌套的条件判断:
#ifndef _DEBUG
#ifdef USE_MYLIB
................
#endif
#endif
wangweicai 2002-12-29
  • 打赏
  • 举报
回复
用汉语讲一下好吗?
看得我头都晕了,都没看明白。
qing_li73 2002-12-29
  • 打赏
  • 举报
回复
The #ifdef and #ifndef Directives
The #ifdef and #ifndef directives perform the same task as the #if directive when it is used with defined( identifier ).

Syntax

#ifdef identifier

#ifndef identifier

is equivalent to

#if defined identifier

#if !defined identifier

You can use the #ifdef and #ifndef directives anywhere #if can be used. The #ifdef identifier statement is equivalent to #if 1 when identifier has been defined, and it is equivalent to #if 0 when identifier has not been defined or has been undefined with the #undef directive. These directives check only for the presence or absence of identifiers defined with #define, not for identifiers declared in the C or C++ source code.

These directives are provided only for compatibility with previous versions of the language. The defined( identifier ) constant expression used with the #if directive is preferred.

The #ifndef directive checks for the opposite of the condition checked by #ifdef. If the identifier has not been defined (or its definition has been removed with #undef), the condition is true (nonzero). Otherwise, the condition is false (0).

Microsoft Specific

The identifier can be passed from the command line using the /D option. Up to 30 macros can be specified with /D.

This is useful for checking whether a definition exists, because a definition can be passed from the command line. For example:

// PROG.CPP
#ifndef test // These three statements go in your source code.
#define final
#endif

CL /Dtest prog.cpp // This is the command for compilation.

END Microsoft Specific
winco 2002-12-29
  • 打赏
  • 举报
回复
#ifdef identifier

#ifndef identifier

is equivalent to

#if defined identifier

#if !defined identifier

You can use the #ifdef and #ifndef directives anywhere #if can be used. The #ifdef identifier statement is equivalent to #if 1 when identifier has been defined, and it is equivalent to #if 0 when identifier has not been defined or has been undefined with the #undef directive. These directives check only for the presence or absence of identifiers defined with #define, not for identifiers declared in the C or C++ source code.

These directives are provided only for compatibility with previous versions of the language. The defined( identifier ) constant expression used with the #if directive is preferred.

The #ifndef directive checks for the opposite of the condition checked by #ifdef. If the identifier has not been defined (or its definition has been removed with #undef), the condition is true (nonzero). Otherwise, the condition is false (0).

从上面MSDN上的话可以看出,他们没有区别:)
用法:
用来定义一个变量,或者头文件,
防止重复定义和重复引用。

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧