什么是C中的##? [英] what is ## in c?

查看:108
本文介绍了什么是C中的##?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这段代码:

  #define kthread_create(threadfn,data,namefmt,arg ...)\ 
kthread_create_on_node(threadfn,data,-1,namefmt,## arg)




  1. ## 代表什么?

  2. ## 当它出现在宏外面时


解决方案

其他答案,这实际上是GCC扩展。当直接粘贴变量参数时,如果没有额外的参数传递,就会出现问题。因此,当与 __ VA_ARGS __ 一起使用或者一个可变参数变量(用 argname声明)时,GCC会使 ## ... )。如果它包含一个值,则粘贴;如果没有,则删除前面的逗号。



此扩展的文档是这里


其次,'##'令牌粘贴当放置在逗号和可变参数之间时,操作符具有特殊含义。如果你写了

  #define eprintf(format,...)fprintf(stderr,format,## __ VA_ARGS__)

,并且在使用eprintf宏时省略变量参数,然后在'##'之前的逗号将会被删除。如果您传递了空参数,则不会发生这种情况,如果前面的'##'之前的标记不是逗号,也不会发生。

c $ c> eprintf(success!\\\

==> fprintf(stderr,success!\\\
);

上面的解释对于唯一的宏参数是可变参数参数的情况是不明确的,因为它试图区分根本没有论证是否是一个空洞的论证或缺少的论证是没有意义的。在这种情况下,C99标准清楚地表明逗号必须保持不变,但是现有的GCC扩展用于吞下逗号。因此,CPP在符合特定C标准时保留逗号,否则将丢弃它。



I have seen this snippet:

#define kthread_create(threadfn, data, namefmt, arg...) \
    kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)

  1. what does ## stand for ?
  2. what is the meaning of ## when it appears out of a macro ?

解决方案

Contrary to the other answers, this is actually GCC extension. When pasting variable args directly in, a problem occurs if no extra args were passed. Thus, GCC makes ## when used with __VA_ARGS__ or a varargs variable (declared with argname...). To paste if it contains a value, or remove the previous comma if not.

The documentation for this extension is here:

Second, the '##' token paste operator has a special meaning when placed between a comma and a variable argument. If you write

#define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)

and the variable argument is left out when the eprintf macro is used, then the comma before the '##' will be deleted. This does not happen if you pass an empty argument, nor does it happen if the token preceding '##' is anything other than a comma.

eprintf ("success!\n")
      ==> fprintf(stderr, "success!\n");

The above explanation is ambiguous about the case where the only macro parameter is a variable arguments parameter, as it is meaningless to try to distinguish whether no argument at all is an empty argument or a missing argument. In this case the C99 standard is clear that the comma must remain, however the existing GCC extension used to swallow the comma. So CPP retains the comma when conforming to a specific C standard, and drops it otherwise.

这篇关于什么是C中的##?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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