C语言 目标C中#define的含义是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3368665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of the #define in the objective C?
提问by Questions
I want to ask a question about the objective-C or may be the Clanguage. I want to ask what is the meaning of the following code of #define? Is it like to declare a variable?
我想问一个关于objective-C或者可能是C语言的问题。请问下面的代码是#define什么意思?就像声明一个变量一样?
#define kMountainNameString @"name"
#define kMountainHeightString @"height"
#define kMountainClimbedDateString @"climbedDate"
回答by seand
It's a simple text substitution macro. Works the same as in C, C++.
这是一个简单的文本替换宏。与 C、C++ 中的工作方式相同。
Where kMountainNameString appears the compiler will "paste in" @"name". Technically speaking this occurs before the compiler by a mechanism called the preprocessor.
在 kMountainNameString 出现的地方,编译器将“粘贴”@“name”。从技术上讲,这是通过一种称为预处理器的机制在编译器之前发生的。
回答by psicopoo
#defineis a preprocessor directive inherited from C that takes the form
#define是从 C 继承的预处理器指令,其形式为
#define identifier value
In general, it is used to tell the preprocessor to replace all instances of identifierin the code with the given text before passing it on to the compiler. Identifiers can also be defined without values to be used as compiler flags to prevent multiple definitions of the same variables, or to branch on machine details that will not change during execution. For example, to pass different code to the compiler based on the architecture of your processor you could do something like:
通常,它用于告诉预处理器identifier在将代码传递给编译器之前用给定文本替换代码中的所有实例。标识符也可以在没有值的情况下定义,用作编译器标志,以防止对同一变量进行多次定义,或者在执行期间不会更改的机器细节上进行分支。例如,要根据处理器的架构将不同的代码传递给编译器,您可以执行以下操作:
#ifdef INTEL86
//some 32-bit code
#else
//some 64-bit code
#endif
When assigning values in these definitions, it's often a good idea to surround the value with parentheses so as to preserve it as one unit, regardless of the context it exists in.
在这些定义中分配值时,用括号将值括起来通常是一个好主意,以便将其保留为一个单位,而不管它存在于何种上下文中。
For example, #define FOO 3 + 7has a different result than #define FOO (3 + 7)on the result of the following line, due to the order of arithmetic operations:
例如,由于算术运算的顺序,其结果与以下行的结果#define FOO 3 + 7不同#define FOO (3 + 7):
a = 3 * FOO
See this linkfor more details on preprocessor directives in general or this linkfor information more focused on Objective C.
回答by Plumenator
The preprocessor replaces all occurences of kMountainNameString with @"name" before compilation begins.
在编译开始之前,预处理器将所有出现的 kMountainNameString 替换为 @"name"。
回答by Kumar Alok
#define is the preprocessor directive in c and c++ language.
#define 是 c 和 c++ 语言中的预处理器指令。
It is used to define the preprocessor macros for the texts. The #define is used to make substitutions throughout the file in which it is located.
它用于定义文本的预处理器宏。#define 用于在它所在的整个文件中进行替换。
#define <macro-name> <replacement-string>

