c语言中数据的格式化输出

 

001、输出整型数据,直接输出

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c
#include <stdio.h>

int main(void)
{
        printf("[%d]\n", 123);

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk
[123]
复制代码

 。

 

002、指定字段宽度

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c 
#include <stdio.h>

int main(void)
{
        printf("[%10d]\n", 123);   /* 指定字段最小的宽度  */

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk
[       123]
复制代码

 

003、设置左对齐

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c
#include <stdio.h>

int main(void)
{
        printf("[%-10d]\n", 123);   //左对齐, 加-即可

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk
[123       ]
复制代码

 

 

004、设置占位符0

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c
#include <stdio.h>

int main(void)
{
        printf("[%010d]\n", 123);   // 指定字符宽度为10, 这只占位符0

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk
[0000000123]
复制代码

 

005、给int指定精度

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c
#include <stdio.h>

int main(void)
{
        printf("[%.8d]\n", 123);   // 给int型指定精度, 相当于%08d

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk
[00000123]
复制代码

 。

 

posted @   小鲨鱼2018  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-07-29 使用基因型数据计算近交系数
2022-07-29 利用基因型数据构建G矩阵
2022-07-29 R 脚本实现 xx.hmp.txt格式数据转换为plink格式
2022-07-29 R语言中 gsub函数应用于数据框中
2022-07-29 linux shell脚本实现 xx.hmp.txt格式数据转换为plink格式
2022-07-29 linux 中如何匹配非空字符
2021-07-29 awk命令末尾数字的作用
点击右上角即可分享
微信分享提示