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]
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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命令末尾数字的作用