其它常見用法就不再敘述了
特殊用法
對於m.n的格式還可以用如下方法表示(例)
char ch[20];
printf("%-*.*s\n",m,n,ch);
char ch[20];
printf("%-*.*s\n",m,n,ch);
備註:
“-”:表示左對齊
“m”:表示字符串總寬度
“n”:表示輸出個數
這種用法的好處是可以在語句之外對參數m和n賦值,從而控制輸出格式。
這種用法的好處是可以在語句之外對參數m和n賦值,從而控制輸出格式。
例子:
#define WID_DST(af) 26 /* width of destination column */
#define WID_GW(af) 18 /* width of gateway column */
printf("%-*.*s ", WID_DST(af), WID_DST(af), "Destination");
printf("%-*.*s %-6.6s %s\n",WID_GW(af), WID_GW(af), "Gateway","Flags","Interface");
#define WID_GW(af) 18 /* width of gateway column */
printf("%-*.*s ", WID_DST(af), WID_DST(af), "Destination");
printf("%-*.*s %-6.6s %s\n",WID_GW(af), WID_GW(af), "Gateway","Flags","Interface");
輸出結果:
//Destination Gateway Flags Interface