偶尔看到C代码printf("%.*s",dataL,data);
,对printf中的格式化字符串"%.*s"有点不解。
查看了http://www.cplusplus.com/reference/cstdio/printf/文档后,有所理解。
| width |description |—– | (number) |Minimum number of characters to be printed. If the value tobe printed is shorter than this number, the result is padded withblank spaces. The value is not truncated even if the result islarger. | * |The width is not specified in the format string, but as an additional integer value argument preceding theargument that has to be formatted.
| .precision |description
|—–
| .number |For integer specifiers (d, i, o,u, x, X): precision specifies theminimum 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 a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
For g and G specifiers: This is the maximumnumber of significant digits to be printed.
For s: this is the maximum number of characters to beprinted. By default all characters are printed until the ending null character is encountered.
If the period is specified without an explicit value for precision , 0 is assumed.
| .* |The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
个人理解如下:
- 格式化字符串没有指定固定宽度/精确度,而是使用*,代表接受整数参数。
- 对于字符串打印,宽度代表最小打印字符个数。如果字符串相对短的话,则填充空格。如果字符串相对长的话,也不会截断。
- 对于字符串打印,精度代表最大打印字符个数。如果字符串相对短的话,不会填充;如果字符串相对长的话,截断。
示例代码testPrintf.cpp如下: 测试结果:
Hello123|
Hello123|
Hello123 |
Hello123|
Hello123|
Hello123|
Hello123|
Hello|
Hello|
Hello123|
Hello|
我的理解是正确的。此外光指定精度时,使用"-“标志指定右对齐没起作用。