printf 似乎忽略了字符串精度

发布于 2024-08-06 16:44:29 字数 1662 浏览 6 评论 0原文

所以,我有点难受。根据我系统上的 man 3 printf,字符串格式 "%5s" 应使用指定的精度来限制从给定字符串参数打印的字符数。

% man 3 printf
PRINTF(3)                BSD Library Functions Manual                PRINTF(3)

NAME
     printf, fprintf, sprintf, snprintf, asprintf, vprintf, vfprintf,
     vsprintf, vsnprintf, vasprintf -- formatted output conversion

...
     s       The char * argument is expected to be a pointer to an array of
             character type (pointer to a string).  Characters from the array
             are written up to (but not including) a terminating NUL charac-
             ter; if a precision is specified, no more than the number             
             specified are written.  If a precision is given, no null
             character need be present; if the precision is not specified, or
             is greater than the size of the array, the array must contain a
             terminating NUL character.

但我的测试代码并没有证实这一点:

#include <stdio.h>
int main()
{
        char const * test = "one two three four";
        printf("test: %3s\n", test);
        printf("test: %3s\n", test+4);
        printf("test: %5s\n", test+8);
        printf("test: %4s\n", test+14);
        return 0;
}

它输出

test: one two three four
test: two three four
test: three four
test: four

When I think I should be getting

test: one
test: two
test: three
test: four

Am IDothing bad, 或者手册页只是在骗我?

仅供参考:我知道我(通常)可以破解字符串,并插入临时 '\0' 来终止字符串(除非它是 char const * ,如下所示,我必须复制它),但它是一个 PITA(特别是如果我试图在同一个 printf 中打印某些东西的两半),并且我想知道为什么精度被忽略。

So, I'm a bit stymied. According to man 3 printf on my system, the string format "%5s" should use the specified precision to limit the number of characters printed from the string argument given.

% man 3 printf
PRINTF(3)                BSD Library Functions Manual                PRINTF(3)

NAME
     printf, fprintf, sprintf, snprintf, asprintf, vprintf, vfprintf,
     vsprintf, vsnprintf, vasprintf -- formatted output conversion

...
     s       The char * argument is expected to be a pointer to an array of
             character type (pointer to a string).  Characters from the array
             are written up to (but not including) a terminating NUL charac-
             ter; if a precision is specified, no more than the number             
             specified are written.  If a precision is given, no null
             character need be present; if the precision is not specified, or
             is greater than the size of the array, the array must contain a
             terminating NUL character.

But my test code doesn't confirm this:

#include <stdio.h>
int main()
{
        char const * test = "one two three four";
        printf("test: %3s\n", test);
        printf("test: %3s\n", test+4);
        printf("test: %5s\n", test+8);
        printf("test: %4s\n", test+14);
        return 0;
}

It outputs

test: one two three four
test: two three four
test: three four
test: four

When I think I should be getting

test: one
test: two
test: three
test: four

Am I doing something wrong, or is the man page just lying to me?

FYI: I know I could (in general) hack the string, and insert temporary '\0' to terminate the string (except when it's a char const *, like here, I'd have to copy it instead), but it's a PITA (especially if I'm trying to print two halves of something in the same printf), and I want to know why the precision is being ignored.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2024-08-13 16:44:29

您没有设置精度,而是设置字段宽度。在格式规范中,精度始终以. 开头。

printf("test: %.3s\n", test);

You're not setting the precision, you're setting the field width. The precision always starts with a . in the format specification.

printf("test: %.3s\n", test);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文