问题描述
我也遇到了的#define
pre处理器的指令之前,同时学习C,然后也遇到了它在某些code我读。但是,除了用它来替代一定的常量和定义宏,我不是真的understook在那里没有身体或令牌字符串中使用的特殊情况。
I have encountered the #define
pre-processor directive before while learning C, and then also encountered it in some code I read. But apart from using it to definite substitutions for constants and to define macros, I've not really understook the special case where it is used without a "body" or token-string.
就拿这一行:
#define OCSTR(X)
就这样!有什么能使用这样或更好的,如果是这样的使用的#define
有必要吗?
推荐答案
这是在两种情况下使用。第一个也是最常见的涉及
条件编译:
This is used in two cases. The first and most frequent involvesconditional compilation:
#ifndef XYZ
#define XYZ
// ...
#endif
您已经肯定用这个自己的include防范,但它也可以是
用于诸如系统的依赖关系:
You've surely used this yourself for include guards, but it can also beused for things like system dependencies:
#ifdef WIN32
// Windows specific code here...
#endif
(在此情况下,WIN32更可能在命令行上定义的,但它
也可以在config.hpp
文件中定义。)这通常会
只涉及类似对象的宏(不带参数列表或
括号中)。
(In this case, WIN32 is more likely defined on the command line, but itcould also be defined in a "config.hpp"
file.) This would normallyonly involve object-like macros (without an argument list orparentheses).
第二是条件编译的结果。某物
这样的:
The second would be a result of conditional compilation. Somethinglike:
#ifdef DEBUG
#define TEST(X) text(X)
#else
#define TEST(X)
#endif
这让写的东西,如:
TEST(X);
将调用的函数,如果 DEBUG
的定义,如果什么都不做
不是。
which will call the function if DEBUG
is defined, and do nothing if itisn't.
这篇关于有什么用例的#define必要不令牌字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!