在c语言编程宏评价 [英] Macros evaluation in c programming language

查看:101
本文介绍了在c语言编程宏评价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  什么“ STR的#define(一)#一”的怎么办?


 的#include<&stdio.h中GT;
  的#define F(A,B)的printf(是)
  的#define克(一)#a的
  的#define小时(一)克(一)  诠释的main()
  {
        的printf(%S \\ n,H(F(1,2)));
        的printf(%S \\ N,G(F(1,2)));  }

有人可以解释为什么输出两个不同的printf()语句。


解决方案

的输出是不同的,因为在其中preprocessor做事的顺序,这是在部分6.10.3中所述(以及以下)中的C99标准。特别是,这句话从6.10.3.1/1:


  

在替换列表中的参数,除非由 ## preceded preprocessing令牌或后跟一个 ## preprocessing令牌,由相应的参数全部换成宏其中包含已扩大了。


因此​​,在第一线,扩大了 ^ h 调用时,参数 F(1,2)扩展的的它取代 ^ h 的参数 A 。在只进场后,当时的所有输出重新扫描被认为是所产生的先按g 的调用。

但在第二行中,立即看到以上报价的,除非preceded通过......条款触发不同的行为。

又见相关的C-FAQ条目

Possible Duplicate:
What does “#define STR(a) #a” do?

#include <stdio.h>
  #define f(a,b) printf("yes")
  #define g(a)   #a
  #define h(a) g(a)

  int main()
  {
        printf("%s\n",h(f(1,2)));
        printf("%s\n",g(f(1,2)));

  }

Can somebody explain why output is different for both printf() statements.

解决方案

The output is different because of the order in which the preprocessor does things, which is described in section 6.10.3 (and those following) in the C99 standard. In particular, this sentence from 6.10.3.1/1:

A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token, is replaced by the corresponding argument after all macros contained therein have been expanded.

So in the first line, when expanding the invocation of h, the argument f(1,2) is expanded before it replaces h's parameter a. The # only comes into play later when the resulting invocation of g is seen when the output of all that is rescanned.

But on the second line, the # is seen immediately and the "unless preceded by..." clause of the quotation above triggers the different behaviour.

See also the relevant C-FAQ entry.

这篇关于在c语言编程宏评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆