什么是'#'正是用C呢? [英] What does '#' exactly do in C?
本文介绍了什么是'#'正是用C呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个计划。
#include <stdio.h>
#define f(a,b) a##b
#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)));
return 0;
}
这个程序正常工作,并给予作为输出:
This program working properly and giving output as:
12
f(1, 2)
我不明白编译器如何给这个输出。
I don't understand how compiler giving this output.
什么是#中的函数 A ## b
和#A
?
推荐答案
让我分解为您:
#define f(a,b) a##b //2 this macro is evaluated first with a = 1 and b = 2 it concatenates them and returns 12
#define g(a) #a //4 g turns 12 into "12" (string)
#define h(a) g(a) //3 back to h which now has a = 12 and call g()
int main()
{
printf("%s\n",h(f(1,2)));//1 printf calls the macro h() and gives it the macro f() as an argument
printf("%s\n",g(f(1,2)));// g here turns f(1,2) into "f(1,2)" (string)
return 0;
}
这篇关于什么是'#'正是用C呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文