示例
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> //C99特性
#include <string.h>
#include <math.h>
#include <ctype.h>
// 变参宏: __VA_ARGS__
// ...只能放在最后
#define PR(X, ...) printf("Message " #X ": "__VA_ARGS__)
// 对于简单函数,通常使用宏定义, 可以减少程序在函数中跳转,从而提高运行效率.
// 在嵌套循环中使用宏更有助于提高效率
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define ABS(X) ((X) < 0 ? -(X) : (X))
#define ISSIGN(X) ((X) == '+' || (X) == '-' ? 1 : 0)
int main(int argc, char* argv[])
{
double x = 48;
double y;
y = sqrt(x);
//宏展开:printf("Message" "1" ": " "x = %g\n", x);
//字符串串联后: printf("Message 1: x = %g\n", x);
PR(1, "x = %g\n", x);
//宏展开:
PR(2, "x = %.2f, y = %.4f\n", x, y);
system("pause");
return 0;
}
运行测试