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

wangweicai 2002-12-29 01:22:17
谁知道#ifndef 和#if !define 的区别?
如果没有区别的话,应该怎么用?
...全文
1353 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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上的话可以看出,他们没有区别:)
用法:
用来定义一个变量,或者头文件,
防止重复定义和重复引用。
资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 在计算机科学领域,编译原理是研究如何将编程语言转化为机器可执行代码的理论基础。其中,三地址代码(Three-Address Code,TAC)作为一种中间表示形式,在编译器设计中经常被使用,尤其是在生成目标代码的阶段。本文将深入探讨三地址代码的概念、生成器的工作原理及其在编译过程中的作用。 三地址代码是一种简单的低级抽象语法树(AST)表示,每条指令涉及三个操作数,通常包括两个源操作数和一个目的操作数。这种格式简化了代码优化和目标代码生成的复杂性。例如,一个简单的算术表达式“x = y + z”在三地址代码中可能表示为: 在这个例子中,“t1”是一个临时变量,存储了“y + z”的结果,然后这个结果被赋值给“x”。 生成三地址代码的过程通常发生在编译器的中间阶段,即语法分析之后,语义分析之前。这个阶段称为“代码生成”或“中间代码生成”。编译器通过词法分析器处理源代码,将其转化为标记流;接着,语法分析器根据上下文无关文法将标记流解析成抽象语法树。三地址代码生成器就是在这个阶段介入,它遍历AST,为每个节点生成对应的三地址指令。 在Turbo C3.0这样的编译器环境下,开发者可以实现自己的三地址代码生成器。虽然Turbo C3.0是一款较老的编译器,但其C语言编译器设计原理依然适用于现代编译器开发。开发过程中,我们需要考虑如下关键点: 符号表管理:符号表记录了程序中所有标识符的类型、作用域和关联地址,对于生成三地址代码至关重要,因为它提供了关于操作数的类型信息。 数据类型转换:编译器必须处理不同数据类型的运算,确保它们在三地址代码中正确表示。例如,整型与浮点型之间的转换需要特别处理。

65,186

社区成员

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

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