C语言-#和##说明



问题来源:在读marlin程序时遇到这种表达,对于我C语言菜鸟来说瞬间懵逼,上网查了下,恍然大悟,现总结如下。

/// set pin as input
#define _SET_INPUT(IO) do {DIO ##  IO ## _DDR &= ~MASK(DIO ## IO ## _PIN); } while (0)
/// set pin as output
#define _SET_OUTPUT(IO) do {DIO ##  IO ## _DDR |=  MASK(DIO ## IO ## _PIN); } while (0)

#表示将后面的宏参数进行字符串化。

##表示连接前后两个变量(符号)。


下面的代码表达的比较清楚:

#include <iostream>
void quit_command(){
    printf("I am quit command\n");
}   
void help_command(){
    printf("I am help command\n");
}   
struct command
{
    char * name;
    void (*function) (void);
};  
#define COMMAND(NAME) {#NAME,NAME##_command}
#define PRINT(NAME) printf("token"#NAME"=%d\n", token##NAME)
main(){
    int token9=9;
    PRINT(9);
    struct command commands[] = {
        COMMAND(quit),
        COMMAND(help),
    };  
    commands[0].function();
}

输出为:

token9=9
I am quit command

参考:
2.http://blog.csdn.net/ce123/article/details/8961518
3.http://gcc.gnu.org/onlinedocs/cpp/Stringification.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值