SCO Unix关于预处理宏的介绍

转自http://uw713doc.sco.com/en/SDK_cprog/_Preprocessing_Directives.html

 

Preprocessing directives

Preprocessing operators

The preprocessing operators are evaluated left to right, without any defined precedence.


#
A macro parameter preceded by the  # preprocessing operator has its corresponding unexpanded argument tokens converted into a string literal. (Any double quotes and backslashes contained in character constants or part of string literals are escaped by a backslash). The  # character is sometimes referred to as the ``stringizing'' operator. This rule applies only within function-like macros.

##
If a replacement token sequence contains a  ## operator, the  ## and any surrounding white space are deleted and adjacent tokens are concatenated, creating a new token. This occurs only when the macro is expanded. See  ``Macro definition and expansion''

Macro definition and expansion

  •  

  • An object-like macro is defined with a line of the form:
    #define identifier token-sequence[opt]
    
    where identifier will be replaced with token-sequence wherever identifier appears in regular text.

     

  • A function-like macro is defined with a line of the form:
    #define identifier( identifier-list[opt] )  token-sequence[opt]
    
    where the macro parameters are contained in the comma-separated identifier-list. The token-sequence following the identifier list determines the behavior of the macro, and is referred to as the replacement list. There can be no space between the identifier and the ( character. For example:
    #define FLM(a,b) a+b
    
    The replacement-list a+b determines that the two parameters a and b will be added.

     

     

     

     

     

     

     

     

  • A function-like macro can also be defined with a line of the form:
    #define identifier( identifier-list[opt], ... )  token-sequence[opt]
    
    where if the identifier-list is not present, there is also no comma. This form of macro definition accepts zero or more arguments matching the ellipsis. Those arguments, if any, that match the ellipsis in an invocation of this macro replace the special identifier __VA_ARGS__ optionally present in the replacement token sequence.

     

  • A function-like macro is invoked in normal text by using its identifier, followed by a ( token, a list of token sequences separated by commas, and a ) token. For example:
    FLM(3,2)
    

     

  • The arguments in the invocation (comma-separated token sequences) may be expanded, and they then replace the corresponding parameters in the replacement token sequence of the macro definition. Macro arguments in the invocation are not expanded if they are operands of # or ## operators in the replacement string. Otherwise, expansion does take place. For example:

    Assume that M1 is defined as 3:

    #define M1 3
    
    When the function-like macro FLM is used, use of the # or ## operators will affect expansion (and the result), as follows: 

    DefinitionInvocationResultExpansion?
    a+bFLM(M1,2)3+2Yes, Yes
    #aFLM(M1,2)"M1"No
    a##bFLM(M1,2)M12No, No
    a+#aFLM(M1,2)3+"M1"Yes, No

    In the last example line, the first a in a+#a is expanded, but the second a is not expanded because it is an operand of the # operator.

     

  • The number of arguments in the invocation must match the number of parameters in the definition.

     

  • A macro's definition, if any, can be eliminated with a line of the form:
    #undef identifier
    
    There is no effect if the definition doesn't exist.

     

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值