MSDN 경로(MSDN을 설치했을 때)
ms-help://MS.MSDNQTR.2004JAN.1033/vclang/html/_predir_The_.23.define_Directive.htm

Syntax


#define identifier token-stringopt
#define identifier[( identifieropt, ... , identifieropt )] token-stringopt


identifier를 token string으로 교체!!
identifier는 토큰의 형태일 경우에만 바뀜...
#if ,#ifdef 와도 관련..

token string은 token 몇 개가 연속된 것(series of tokens)

파라미터들이 token-string에 나올 수 있음...(2번째 경우..함수처럼 사용 가능)

그리고, newline 문자 바로 전에 backslash(\)를 사용하여서 line 연결자로 사용할 수 있습니다. 2줄 이상 되는 경우에 사용 가능한 듯. formal parameter의 범위는 token-string을 끝내는 new line까지 확장됩니다.

참고로 #define에 사용되어서, ,로 구분되는것이 formal parameter, 소스에 있으면서, #define에 매칭되어 실제로 바뀌는 파라미터가 actual parameter입니다.

매크로가 2번째 형식으로 정의되었을 때, argument list 뒤에 오는 텍스트 인스턴스들은 macro call을 구성한다. 소스 파일에서 Identifier의 인스턴스 뒤에 오는 actual arguments들은 해당하는, 매크로 정의에서의 formal parameter로 match된다. 각각의 토큰 스트링에서의 #, #@, ## 뒤에 오거나 ## 앞에 오지 않는 formal paramenter들은 해당하는 actual parameter로 변환된다.

일단 여기까지 필요한 부분이라 번역했고, 추후 추가될 예정입니다.



===========================================================
원문
You can use the #define directive to give a meaningful name to a constant in your program. The two forms of the syntax are:

#define identifier token-stringopt
#define identifier[( identifieropt, ... , identifieropt )] token-stringopt
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. (See C++ 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 (\) immediately 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

Trackback Address :: http://www.dreamjr.org/tt/trackback/7

  1. dissertations methodology 2012/04/05 14:43 댓글주소 | 수정 | 삭제 | 댓글

    Your post is very informative. I admire your efforts. I will keep on coming here to see new updates .Thanks for sharing this information.