预处理#define中的符号和字符串问题

预处理#define中的符号和字符串问题

  • # 字符串化,即把传进宏的参数转变成相应string
  • ## 标识符胶水,即把两个token黏合为一个token
/*
 * 1.字符串化, Creating Strings from Macro Arguments: The # Operator
 *   Within the replacement part of a function-like macro, the # symbol becomes a preprocessing operator that converts tokens into strings.
 *   把 #x 转变成字符串 "x", 其中x是传进宏的参数
 *
 * 2.标识符黏合, Preprocessor Glue: The ## Operator
 *   The ## operator combines two tokens into a single token
 */

#include <stdio.h>

#define TEST1(x) printf("the square of x is %d\n", (x)*(x))
#define TEST2(x) printf("the square of " #x " is %d\n", (x)*(x))
#define PRINT_XN(n) printf("a" #n " = %d\n", a ## n)

int main(int argc, char const *argv[])
{
    int i = 4;

    TEST1(i);
    TEST1(2 + 3);

    TEST2(i);
    TEST2(2 + 3);  // printf("the square of ""2 + 3"" is %d\n", (2 + 3)*(2 + 3))
    TEST2((2 + 3));

    int a1 = 50;
    int a2 = 60;

    PRINT_XN(1);  // printf("a1 = %d\n", a1);
    PRINT_XN(2);
    PRINT_XN(1+1);

    printf("good" "bye" "\n");

    return 0;
}


// 运行结果:
$ ./a.out
the square of x is 16
the square of x is 25
the square of i is 16
the square of 2 + 3 is 25
the square of (2 + 3) is 25
a1 = 50
a2 = 60
a1+1 = 51
goodbye

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值