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



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM