ç$ P $#pprocessor和##运营商 [英] C preprocessor # and ## operators

查看:164
本文介绍了ç$ P $#pprocessor和##运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助 C99标准文档已经在部分下面的例子涉及到## preprocessing运营商:

The C99 standard document has the following example in the section related to the ## preprocessing operator:

在下面的代码片段:

#define hash_hash # ## #
#define mkstr(a) # a
#define in_between(a) mkstr(a)
#define join(c, d) in_between(c hash_hash d)

char p[] = join(x, y); // equivalent to
                       // char p[] = "x ## y";


  
  

扩大生产,在不同的
  阶段:

The expansion produces, at various stages:

join(x, y)
in_between(x hash_hash y)
in_between(x ## y)
mkstr(x ## y)
"x ## y"

在换句话说,扩大hash_hash
  产生一个新的标记,由
  两个相邻的尖锐的迹象,但这种新的
  令牌不是##运营商。

In other words, expanding hash_hash produces a new token, consisting of two adjacent sharp signs, but this new token is not the ## operator.

我不明白为什么hash_hash的替代产生##,而不是##或#,#。什么样的作用是前后双哈希播放?

I don't understand why the substitution of hash_hash produces ## and not "##" or "#""#". What role are the single hashes before and after the double hash playing?

任何响应大大AP​​ preciated。

Any responses greatly appreciated.

推荐答案

## ####就像在这个前pression转义序列。它加到最左边和最右边的来最终产生令牌 ## 。简单地定义宏为 ## 将导致一个错误,因为连接运算符需要两个操作数。

The ## in # ## # acts like an escape sequence in this expression. It concatenates the leftmost and the rightmost # to finally produce the token ##. Simply defining the macro as ## would cause an error since the concatenation operator expects two operands.

这篇关于ç$ P $#pprocessor和##运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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