#include <stdio.h>
int main()
{
int a = 1234;
float f = 123.456;
char s[]="Hello world!";
printf("%8d,%-8d\n",a,a);
printf("%10.2f,%-10.1f\n",f,f);
printf("%10.5s,%-10.3s\n",s,s);
return 0;
}
输出结果:
1234,1234
123.46,123.5
Hello,Hel
演示格式符 “0”、“+”、“#" 用法:
#include <stdio.h>
int main()
{
int a = 1234;
float f = 123.456;
printf("%08d\n",a);
printf("%010.2f\n",f);
printf("%0+8d\n",a);
printf("%0+10.2f\n",f);
a = 123;
printf("%o,%#o,%X,%#X\n",a,a,a,a);
return 0;
}