整型-%d
浮点型-%f
浮点型-%e
整型-%d
int a = 123;float b = 3.14, c = -3.14;long d = 65535;printf("a1 is %d\n",a);printf("a2 is %2d\n",a);printf("a3 is %3d\n",a);printf("a4 is %4d\n",a);
输出结果:
浮点型-%f
printf("******************\n");printf("b11 is %f\n",b);printf("b12 is %.1f\n",b);printf("b13 is %4f\n",b);printf("b14 is %4.1f\n",b);printf("b15 is %4.2f\n",b);printf("b16 is %3.1f\n",b);printf("b17 is %6.1f\n",b);printf("b18 is %6.2f\n",b);printf("b19 is %6.5f\n",b);printf("b21 is %6.6f\n",b);printf("b22 is %-6.1f\n",b);printf("b23 is %-4.1f\n",b);printf("b24 is %-6.4f\n",b);printf("b25 is %-4.2f\n",b);
输出结果:
printf("******************\n");printf("c11 is %f\n",c);printf("c12 is %.1f\n",c);printf("c13 is %4f\n",c);printf("c14 is %4.1f\n",c);printf("c15 is %4.2f\n",c);printf("c16 is %3.1f\n",c);printf("c17 is %6.1f\n",c);printf("c18 is %6.2f\n",c);printf("c19 is %6.5f\n",c);printf("c20 is %6.6f\n",c);printf("c21 is %-6.1f\n",c);printf("c22 is %-4.1f\n",c);printf("c23 is %-6.4f\n",c);printf("c24 is %-4.2f\n",c);
输出结果:
%m.nf总结:m是最小数据宽度,包含负号和小数点,n是小数点后的数字位数。当实际数值位数>m时,全部输出,<m时,靠右左补空格。(若是%-m.nf,则靠左右补空格)
浮点型-%e
printf("b26 is %4.2e\n",b);printf("b27 is %9.2e\n",b);printf("b28 is %9.5e\n",b);printf("b29 is %8.2e\n",b);printf("b31 is %8.6e\n",b);printf("b32 is %12.4e\n",b);printf("b33 is %-9.2e\n",b);printf("b34 is %-12.2e\n",b);
输出结果:
printf("c25 is %4.2e\n",c);printf("c26 is %9.2e\n",c);printf("c27 is %9.5e\n",c);printf("c28 is %8.2e\n",c);printf("c29 is %8.6e\n",c);printf("c30 is %12.4e\n",c);printf("c31 is %-9.2e\n",c);printf("c32 is %-12.2e\n",c);
输出结果:
%m.ne总结:m为整个数据宽度(包含指数部分5位,即e+000),包含负号,n指小数点后的数字位。若数值位数>m,则全部输出,若<m,则靠右左补空格。