C 语言常用的预处理-宏函数

#include <stdio.h>


//  宏函数    三目运算符
#define MAX(A, B) A>B?A:B

//宏函数  多行 添加\直接回车
#define LOOP(FROM, TO, CONTENT)\
for(int i=FROM;i<TO;i++){\
CONTENT\
}

//宏函数不需要确定参数类型 普通函数如下
int _max(int a, int b) {
    return a > b ? a : b;
}

//有相同前缀
void cSayHi() {
    printf("Hi C\n");
}

void cSayHello() {
    printf("Hello C\n");
}
//宏函数参数连接
#define callc(NAME) c##NAME() //callc 任意更改

//宏的可变参数
#define LOG(LEVEL, FORMAT, ...) printf(LEVEL);printf(FORMAT,__VA_ARGS__);//
#define LOG_1(FORMAT, ...) printf("LOG:");printf(FORMAT,__VA_ARGS__);
#define LOG_2(FORMAT, ...) LOG("LOG:",FORMAT,__VA_ARGS__);

int main() {

    printf("Max num is %f\n", MAX(1.3, 3.3));

    printf("Max num is %d\n", _max(1, 3));

    LOOP(2, 10, printf("Current Index is %d\n", i);)

    callc(SayHello);

    LOG("LOG:", "Hello %s %d\n", "World", 100);
    LOG_1("Hello %s %d\n", "World", 100);
    LOG_2("Hello %s %d\n", "World", 100);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值