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