# 和 ## 运算符

#运算符

1.#运算符用于在预处理期将宏参数转换为字符串。
2.#的作用是在预处理期间完成的,因此只有在宏定义中有效
3.编译器不知道 # 的转换作用

#include <stdio.h>

#define STRING(x) #x

int main()
{
    printf("%s\n",STRING(Hello World!));
    printf("%s\n",STRING(100));
    printf("%s\n",STRING(while));
    printf("%s\n",STRING(return));

    return 0;
}

经过预处理器,处理过后的程序如下所示:

//gcc -E main.c -o test.i
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "main.c"
##include <stdio.h>
int main()
{
    printf("%s\n","Hello World!");
    printf("%s\n","100");
    printf("%s\n","while");
    printf("%s\n","return");

    return 0;
}

想一个问题,怎么样在程序中,把函数调用的函数名字打印出来?
这里需要要用到宏,而且必须用到 #,否者用函数是实现不了的。

#include <stdio.h>

#define CALL(f,p) (printf("Call function %s\n",#f),f(p))

int square(int n)
{
    return n*n;
}

int func(int x)
{
    return x;
}
 
int main()
{
    int result=0;

    result = CALL(square,4);

    printf("result = %d\n",result);

    result = CALL(func,10);

    printf("result = %d\n",result);

    return 0;
}

程序经过预处理器处理过后,为下面所示:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "main.c"

int square(int n)
{
    return n*n;
}

int func(int x)
{
    return x;
}

int main()
{
    int result=0;

    result = (printf("Call function %s\n","square"),square(4));

    printf("result = %d\n",result);

    result = (printf("Call function %s\n","func"),func(10));

    printf("result = %d\n",result);

    return 0;
}

##运算符

1.##运算符用于在预处理器期间,粘住两个标识符
2.##的连接作用是在预处理器期间完成的,因此只在宏定义中有效
3.编译器不知道##的连接作用

#include <stdio.h>

#define NAME(n) name##n

int main()
{
    int NAME(1);
    int NAME(2);

    NAME(1)=1;
    NAME(2)=2;

    printf("%d\n",NAME(1));
    printf("%d\n",NAME(2));

    return 0;
}

经过预处理器处理之后的代码为:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "main.c"

int main()
{
    int name1;
    int name2;

    name1=1;
    name2=2;

    printf("%d\n",name1);
    printf("%d\n",name2);

    return 0;
}

再来看一个实例程序:

//#include <stdio.h>

#define STRUCT(type) typedef struct _tag_##type type;\
                     struct _tag_##type

STRUCT(Student)
{
    char* name;
    int id;
};

int main()
{
    Student s1;
    Student s2;

    s1.name="s1";
    s1.id=0;

    s2.name="s2";
    s2.id=1;

    printf("s1.name = %s\n",s1.name);
    printf("s1.id = %d\n",s1.id);
    printf("s2.name = %s\n",s2.name);
    printf("s2.id = %d\n",s2.id);

    return 0;
}

同样经过预处理器之后,程序为:

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "main.c"

typedef struct _tag_Student Student; struct _tag_Student
{
    char* name;
    int id;
};

int main()
{
    Student s1;
    Student s2;

    s1.name="s1";
    s1.id=0;

    s2.name="s2";
    s2.id=1;

    printf("s1.name = %s\n",s1.name);
    printf("s1.id = %d\n",s1.id);
    printf("s2.name = %s\n",s2.name);
    printf("s2.id = %d\n",s2.id);

    return 0;
}

小结:
1.#运算符用于在预处理期间将宏参数转化为字符串。
2.##运算符用于在预处理器期间连接两个标识符
3.编译器不知道#和##运算符的存在
4.#和##运算符只在宏定义中有效。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

repinkply

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值