#define的用法
2016-07-18 10:24
381 查看
#define The #define Directive
You can use the #define directive to give a meaningful name to a constant in your program. The two forms of the syntax are:
Syntax
#define identifier token-stringopt
#define identifier[( identifieropt, ... , identifieropt )] token-stringopt
__________________________________________
以上是最主要的语法,说明一下,其他在运用时再理解也不迟:
语法一:
#define 标识符 被标识符代表的字符串 //这种用法很简单,就是一种替换.
语法二:
#define 标识符[(参数1,.....,参数n)] 被标识符代表的字符串 //其中,在"被标识符代表的字符串"中出现的形参将在使用时被实参替代. 就象写函数一样.
补充一点: 在用#define 定义时 ,可以用斜杠("\") 续行.与vb中的下划线(" _")作用同.
比如:
#define add1( x, y ) ( x + y)
也可以表示成 :
#define add1(x,y) \
(x + y )
__________________________________________
The #define directive substitutes token-string for all subsequent occurrences of an identifier in the source file. The identifier is replaced only when it forms a token. (SeeC++ Tokens in the C++ Language Reference.) For instance, identifier is not replaced
if it appears in a comment, within a string, or as part of a longer identifier.
A #define without a token-string removes occurrences of identifier from the source file. The identifier remains defined and can be tested using the #if defined and #ifdef directives.
The token-string argument consists of a series of tokens, such as keywords, constants, or complete statements. One or more white-space characters must separate token-string from identifier. This white space is not considered part of the substituted text,
nor is any white space following the last token of the text.
Formal parameter names appear in token-string to mark the places where actual values are substituted. Each parameter name can appear more than once in token-string, and the names can appear in any order. The number of arguments in the call must match the
number of parameters in the macro definition. Liberal use of parentheses ensures that complicated actual arguments are interpreted correctly.
The second syntax form allows the creation of function-like macros. This form accepts an optional list of parameters that must appear in parentheses. References to the identifier after the original definition replace each occurrence of identifier( identifieropt,
..., identifieropt ) with a version of the token-string argument that has actual arguments substituted for formal parameters.
The formal parameters in the list are separated by commas. Each name in the list must be unique, and the list must be enclosed in parentheses. No spaces can separate identifier and the opening parenthesis. Use line concatenation — place a backslash (\) before
the newline character — for long directives on multiple source lines. The scope of a formal parameter name extends to the new line that ends token-string.
When a macro has been defined in the second syntax form, subsequent textual instances followed by an argument list constitute a macro call. The actual arguments following an instance of identifier in the source file are matched to the corresponding formal
parameters in the macro definition. Each formal parameter in token-string that is not preceded by a stringizing (#), charizing (#@), or token-pasting (##) operator, or not followed by a ## operator, is replaced by the corresponding actual argument. Any macros
in the actual argument are expanded before the directive replaces the formal parameter. (The operators are described in Preprocessor Operators.)
The following examples of macros with arguments illustrate the second form of the #define syntax:
// Macro to define cursor lines
#define CURSOR(top, bottom) ((top) << 8) | bottom))
// Macro to get a random integer with a specified range
#define getrandom(min, max) \
((rand()%(int)(((max) + 1)-(min)))+ (min))
Arguments with side effects sometimes cause macros to produce unexpected results. A given formal parameter may appear more than once in token-string. If that formal parameter is replaced by an expression with side effects, the expression, with its side effects,
may be evaluated more than once. (See the examples under Token-Pasting Operator (##).)
The #undef directive causes an identifier’s preprocessor definition to be forgotten. See The #undef Directive for more information.
If the name of the macro being defined occurs in token-string (even as a result of another macro expansion), it is not expanded.
A second #define for a macro with the same name generates an error unless the second token sequence is identical to the first.
Microsoft Specific
Microsoft C/C++ allows the redefinition of a macro, but generates a warning, provided the new definition is lexically identical to a previous definition. ANSI C considers macro redefinition an error. For example, these macros are equivalent for C/C++ but
generate warnings:
#define test( f1, f2 ) ( f1 * f2 )
#define test( a1, a2 ) ( a1 * a2 )
END Microsoft Specific
This example illustrates the #define directive:
#define WIDTH 80
#define LENGTH ( WIDTH + 10 )
The first statement defines the identifier WIDTH as the integer constant 80 and defines LENGTH in terms of WIDTH and the integer constant 10. Each occurrence of LENGTH is replaced by (WIDTH + 10). In turn, each occurrence of WIDTH + 10 is replaced by the
expression (80 + 10). The parentheses around WIDTH + 10 are important because they control the interpretation in statements such as the following:
var = LENGTH * 20;
After the preprocessing stage the statement becomes:
var = ( 80 + 10 ) * 20;
which evaluates to 1800. Without parentheses, the result is:
var = 80 + 10 * 20;
which evaluates to 280.
Microsoft Specific
Defining macros and constants with the /D compiler option has the same effect as using a #define preprocessing directive at the beginning of your file. Up to 30 macros can be defined with the /D option.
END Microsoft Specific [编辑本段]#define中的
#与## #define GPEBLT_FUNCNAME(basename) (SCODE (GPE::*)(struct GPEBltParms *))&GPE::##basename
在#define中,标准只定义了#和##两种操作。#用来把参数转换成字符串,##则用来连接两个前后两个参数,把它们变成一个字符串。
#include <stdio.h>
#define paster( n ) printf( "token " #n" = %d\n ", token##n )
int main()
{
int token9=10;
paster(9);
return 0;
}
输出为
[leshy@leshy src]$ ./a.out
token 9 = 10
收藏于 2009-06-24
You can use the #define directive to give a meaningful name to a constant in your program. The two forms of the syntax are:
Syntax
#define identifier token-stringopt
#define identifier[( identifieropt, ... , identifieropt )] token-stringopt
__________________________________________
以上是最主要的语法,说明一下,其他在运用时再理解也不迟:
语法一:
#define 标识符 被标识符代表的字符串 //这种用法很简单,就是一种替换.
语法二:
#define 标识符[(参数1,.....,参数n)] 被标识符代表的字符串 //其中,在"被标识符代表的字符串"中出现的形参将在使用时被实参替代. 就象写函数一样.
补充一点: 在用#define 定义时 ,可以用斜杠("\") 续行.与vb中的下划线(" _")作用同.
比如:
#define add1( x, y ) ( x + y)
也可以表示成 :
#define add1(x,y) \
(x + y )
__________________________________________
The #define directive substitutes token-string for all subsequent occurrences of an identifier in the source file. The identifier is replaced only when it forms a token. (SeeC++ Tokens in the C++ Language Reference.) For instance, identifier is not replaced
if it appears in a comment, within a string, or as part of a longer identifier.
A #define without a token-string removes occurrences of identifier from the source file. The identifier remains defined and can be tested using the #if defined and #ifdef directives.
The token-string argument consists of a series of tokens, such as keywords, constants, or complete statements. One or more white-space characters must separate token-string from identifier. This white space is not considered part of the substituted text,
nor is any white space following the last token of the text.
Formal parameter names appear in token-string to mark the places where actual values are substituted. Each parameter name can appear more than once in token-string, and the names can appear in any order. The number of arguments in the call must match the
number of parameters in the macro definition. Liberal use of parentheses ensures that complicated actual arguments are interpreted correctly.
The second syntax form allows the creation of function-like macros. This form accepts an optional list of parameters that must appear in parentheses. References to the identifier after the original definition replace each occurrence of identifier( identifieropt,
..., identifieropt ) with a version of the token-string argument that has actual arguments substituted for formal parameters.
The formal parameters in the list are separated by commas. Each name in the list must be unique, and the list must be enclosed in parentheses. No spaces can separate identifier and the opening parenthesis. Use line concatenation — place a backslash (\) before
the newline character — for long directives on multiple source lines. The scope of a formal parameter name extends to the new line that ends token-string.
When a macro has been defined in the second syntax form, subsequent textual instances followed by an argument list constitute a macro call. The actual arguments following an instance of identifier in the source file are matched to the corresponding formal
parameters in the macro definition. Each formal parameter in token-string that is not preceded by a stringizing (#), charizing (#@), or token-pasting (##) operator, or not followed by a ## operator, is replaced by the corresponding actual argument. Any macros
in the actual argument are expanded before the directive replaces the formal parameter. (The operators are described in Preprocessor Operators.)
The following examples of macros with arguments illustrate the second form of the #define syntax:
// Macro to define cursor lines
#define CURSOR(top, bottom) ((top) << 8) | bottom))
// Macro to get a random integer with a specified range
#define getrandom(min, max) \
((rand()%(int)(((max) + 1)-(min)))+ (min))
Arguments with side effects sometimes cause macros to produce unexpected results. A given formal parameter may appear more than once in token-string. If that formal parameter is replaced by an expression with side effects, the expression, with its side effects,
may be evaluated more than once. (See the examples under Token-Pasting Operator (##).)
The #undef directive causes an identifier’s preprocessor definition to be forgotten. See The #undef Directive for more information.
If the name of the macro being defined occurs in token-string (even as a result of another macro expansion), it is not expanded.
A second #define for a macro with the same name generates an error unless the second token sequence is identical to the first.
Microsoft Specific
Microsoft C/C++ allows the redefinition of a macro, but generates a warning, provided the new definition is lexically identical to a previous definition. ANSI C considers macro redefinition an error. For example, these macros are equivalent for C/C++ but
generate warnings:
#define test( f1, f2 ) ( f1 * f2 )
#define test( a1, a2 ) ( a1 * a2 )
END Microsoft Specific
This example illustrates the #define directive:
#define WIDTH 80
#define LENGTH ( WIDTH + 10 )
The first statement defines the identifier WIDTH as the integer constant 80 and defines LENGTH in terms of WIDTH and the integer constant 10. Each occurrence of LENGTH is replaced by (WIDTH + 10). In turn, each occurrence of WIDTH + 10 is replaced by the
expression (80 + 10). The parentheses around WIDTH + 10 are important because they control the interpretation in statements such as the following:
var = LENGTH * 20;
After the preprocessing stage the statement becomes:
var = ( 80 + 10 ) * 20;
which evaluates to 1800. Without parentheses, the result is:
var = 80 + 10 * 20;
which evaluates to 280.
Microsoft Specific
Defining macros and constants with the /D compiler option has the same effect as using a #define preprocessing directive at the beginning of your file. Up to 30 macros can be defined with the /D option.
END Microsoft Specific [编辑本段]#define中的
#与## #define GPEBLT_FUNCNAME(basename) (SCODE (GPE::*)(struct GPEBltParms *))&GPE::##basename
在#define中,标准只定义了#和##两种操作。#用来把参数转换成字符串,##则用来连接两个前后两个参数,把它们变成一个字符串。
#include <stdio.h>
#define paster( n ) printf( "token " #n" = %d\n ", token##n )
int main()
{
int token9=10;
paster(9);
return 0;
}
输出为
[leshy@leshy src]$ ./a.out
token 9 = 10
收藏于 2009-06-24
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C 语言简单加减乘除运算
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中进制知识汇总
- C语言判断一个数是否是2的幂次方或4的幂次方
- C语言二进制思想以及数据的存储
- C语言中计算正弦的相关函数总结