修饰符和标记的使用
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
const int x=959;
printf("*%d*\n",x);
printf("*%2d*\n",x);
printf("*%10d*\n",x);
printf("*%-10d*\n",x);
printf("*%+10d*\n",x);
return 0;
}
%2d,由于959是占三位数,大于2,C编译自动显示完整数值。
%10d,使得数字一共占十位,位数不够在数字之前用空格补齐。
%-10d,使得数字一共占十位,先显示数字后面不够的位数用空格补齐。
%+10d显示+;
%010d,使得数字一共占十位,位数不够在数字之前用0补齐。
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
const double x=3852.99;
printf("*%f*\n",x);
printf("*%e*\n",x);
printf("*%4.2e*\n",x);
printf("*%4.2f*\n",x);
printf("*%3.1f*\n",x);
printf("*%10.3f*\n",x);
printf("*%10.3E*\n",x);
printf("*%+4.2f*\n",x);
printf("*%07.2f*\n",x);
printf("*%08.2f*\n",x);
printf("*%09.2f*\n",x);
return 0;
}
printf("%4.2e\n",x);
printf("%4.3e\n",x);
显示
3.85e+003
3.853e+003
.之后的数是设置小数点后精确到的数值,具有四舍五入的功能。
printf("%10.2e\n",x);
printf("%11.2e\n",x);
显示
3.85e+003
03.85e+003
0代表空格。