一、宏定义嵌套
宏定义多层嵌套(10 * 10 * 10),printf多次输出。
#include <stdio.h>
// 需要10*10*10次printf
#define B P,P,P,P,P,P,P,P,P,P
#define P L,L,L,L,L,L,L,L,L,L
#define L I,I,I,I,I,I,I,I,I,I,N
#define I printf("%3d ", i++) // 使输出的int型数据以3位的固定位宽输出若不足3位则在前面补空格。
#define N printf("\n")
int main()
{
int i = 1;
B;
return 0;
}
二、宏使用嵌套
简便写法,同样使用多层嵌套:
#include<stdio.h>
// 10层
// 相当于10个x语句
#define A(x) x;x;x;x;x;x;x;x;x;x;
int main ()
{
int n=1;
// A(printf("%d \n", n++)); 相当于10个printf语句
// A( A( printf("%d \n", n++) ) ); 相当于10个“ 10printf语句 ”
A(A(A(printf("%d \n", n++))));
return 0;
}
printf的%2d,%-2d,%.2d,%02d含义: