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;
}


posted @ 2016-12-20 16:32  changchou  阅读(5795)  评论(0编辑  收藏  举报