输出填充字符

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     //%0xld:要是输出小于x位,用0补上,使输出达到x位;否则正常输出。
 7     printf("%02ld
",1);
 8     printf("%02ld
",111);
 9 
10     printf("------
");
11 
12     //%xld:要是输出小于x位,用空格补上,使输出达到x位;否则正常输出。
13     printf("%2ld
",1);
14     printf("%2ld
",111);
15 
16     //要想输出填充其它字符,c只能手动添加
17     //如输出两个整数,不够15位用"*"填充
18     //sprintf构造不同的输出并记录长度
19     char str[100];
20     long s=100000,t=12345,i,j;
21     j=sprintf(str,"%ld %ld",s,t);
22     for (i=j;i<15;i++)
23         str[i]='*';
24     str[15]='';
25     printf("%s
",str);
26     return 0;
27 }
原文地址:https://www.cnblogs.com/cmyg/p/6670950.html