什么是#运营商的宏吗? [英] What does the # operator do in macros?

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

问题描述

#include <stdio.h>

#define foo(x, y) #x #y

int main()
{
    printf("%s\n", foo(k, l));
    return 0;
}

输出:结果
KL

我知道##做串联。从输出似乎也做串联。
我对么?

I know that ## does concatenation. From the output it seems that # also does concatenation. Am I correct?

如果我是正确的话是什么之间 ## 运营商和运营商?

If I am correct then what is the difference between ## operator and # operator?

推荐答案

打开参数为一个字符串。因此,美孚(K,L)变成K,L,这是一样的KL因为在C多重字符串是直接相邻的被视为一个单一的字符串。

# turns the argument into a string. So foo(k, l) becomes "k" "l", which is the same as "kl" because in C multiple string literals that are directly next to each other are treated as a single string literals.

如果并串联,你的printf调用将成为的printf(%S \\ n,KL); 这将产生约误差 KL 没有被定义的。

If # did concatenation, your printf call would become printf("%s\n", kl); which would produce an error about kl not being defined.

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

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