类printf函数中[.*]的巧用场景

本文详细解析了printf函数中[.*]的使用方法,包括如何通过它来设置输出精度,适用于不同的数据类型。并通过一个实际示例展示了如何生成固定长度的随机订单号。

2014-09-24 wcdj


此文章的用法对应之前的一篇文章《类scanf函数中%[*]type的巧用场景》。


printf中[.*]的用法

区别
.precision    description

[1] .number    For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For e, E and f specifiers: this is the number of digits to be printed after the decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

[2] .*    The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

例如:

    char name[]="123456789";
    char *p=name+5;
    printf("%.*s\n",p - name, name);// 12345

    int a=12345;
    printf("%.*d\n",6, a);// 012345  显示化设置精度


一个DEMO:

生成一个固定长度的随机订单号

#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

int main()
{
	string str   = "140924";
	int order    = 12345;

	const int maxlen     = 10;
	char buf[maxlen + 1] = {0};

	snprintf(buf, sizeof(buf), "%.*d", maxlen, order);
	printf("buf[%s]\n", buf);

	str += buf;
	printf("str[%s]\n", str.c_str());

	return 0;
}
/*
output:
buf[0000012345]
str[1409240000012345]
*/



总结:
类scanf()函数里的作用是跳过该参数,而类printf()函数里的作用是用来设置精度。




### C语言实现打印由符号 `'*'` 构成的任意 n 层直角实心三角形 以下展示了如何使用 C 语言编写一个函数来打印由 `'*'` 符号构成的任意 n 层直角实心三角形。核心逻辑在于通过双重循环分别控制行数和列数,其中外部循环决定总共有多少层(即行数),而内部循环则决定了每层中应打印的星号数量。 #### 函数定义与说明 下面是具体的函数实现方式以及解释: ```c #include <stdio.h> // 定义打印直角实心三角形的函数 void printRightAngleTriangle(int layers) { for (int i = 1; i <= layers; ++i) { // 外部循环控制层数(行数) for (int j = 1; j <= i; ++j) { // 内部循环控制每层打印的星号数目 printf("* "); } printf("\n"); // 每完成一层后换行 } } int main() { int n; // 输入要打印的三角形层数 printf("请输入想要打印的直角实心三角形的层数:"); scanf("%d", &n); // 调用函数打印对应的直角实心三角形 printRightAngleTriangle(n); return 0; } ``` 在这个例子中,我们首先包含了标准输入输出库 `<stdio.h>` ,接着定义了一个名为 `printRightAngleTriangle` 的函数用来执行实际的打印工作。这个函数接收一个整型参数 `layers` 表示期望生成的直角三角形有多少层。主函数部分负责获取用户的输入,并调用前述定义好的函数来进行最终的结果展示[^4]。 例如,当用户输入 `6` 作为层数时,上面这段代码将会输出如下形式的直角实心三角形: ``` * * * * * * * * * * * * * * * * * * * * * ``` 值得注意的是,在这里为了美观增加了每个星号之后的一个空格分隔开它们;如果你不需要这样的间隔可以直接去掉 `"* "` 中间的那个空格变成单纯地打印单个星号即可[^5]。 --- ### 关于效率和其他可能改进的地方 虽然上述解决方案已经足够简单明了适用于大多数场景下需求,但在某些特殊情况下还是可能存在进一步优化空间。比如对于特别大尺寸的数据集来说频繁调用 `printf()` 可能会造成一定性能损耗。因此在这种极端条件下考虑采用字符串拼接技术预先准备好全部待显示内容再一次性写入屏幕可能会更加高效一些[^6]。 另外也可以增加错误检测机制确保只有正值才会被接受为有效输入等等增强健壮性的措施。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值