gpt4 book ai didi

c - 如何用变量指定字段宽度?

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:33 25 4
gpt4 key购买 nike

我想知道是否可以使用变量来指定字段宽度,例如:

float g = 123.4567;
int x = 3;
int y = 4;
printf("%(%d).(%d)f", x,y,g);
I want my output to be: "123.4567", basically the same as

printf("%3.4f");

我认为编译器不会读取当前格式,但也许还有另一种方式。

最佳答案

这是关于 C 的问题吗?我看到了 q 标签,但我不确定,因为你谈论的是编译器......

无论如何,在 C 中它是用 * 完成的:

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

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

printf ("Width trick: %*d \n", 5, 10);
Width trick: 10

http://www.cplusplus.com/reference/cstdio/printf/

关于c - 如何用变量指定字段宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22132249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com