在C语言中,如何格式化输出整数?
实现代码
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
int main(void)
{
int i = 15, j = 345, k = 4567;
long long li = 56789LL, lj = 67891234567LL, lk = 23456789LL;
printf_s("i = %d j = %d k = %d i = %6.3d j = %6.3d k = %6.3d\n", i, j, k, i, j, k);
printf_s("i = %-d j = %+d k = %-d i = %-6.3d j = %-6.3d k = %-6.3d\n", i, j, k, i, j, k);
printf_s("li = %d lj = %d lk = %d\n", li, lj, lk);
printf_s("li = %lld lj = %lld lk = %lld\n", li, lj, lk);
system("pause");
return 0;
}
注: -
标志使输出左对齐。
执行上面示例代码,得到以下结果:
i = 15 j = 345 k = 4567 i = 015 j = 345 k = 4567
i = 15 j = +345 k = 4567 i = 015 j = 345 k = 4567
li = 56789 lj = 0 lk = -828242169
li = 56789 lj = 67891234567 lk = 23456789