printf 似乎忽略了字符串精度 [英] printf seems to be ignoring string precision

查看:47
本文介绍了printf 似乎忽略了字符串精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有点受阻.根据我系统上的 man 3 printf ,字符串格式 "%5s" 应该使用指定的精度来限制从给定的字符串参数打印的字符数.><前>% 人 3 打印输出PRINTF(3) BSD 库函数手册 PRINTF(3)名称printf, fprintf, sprintf, snprintf, asprintf, vprintf, vfprintf,vsprintf, vsnprintf, vasprintf -- 格式化输出转换...s char * 参数应该是一个指向数组的指针字符类型(指向字符串的指针).数组中的字符被写入(但不包括)一个终止的 NUL 字符三;如果指定了精度,则不超过数字指定的写.如果给出精度,则不为空性格需要在场;如果未指定精度,或大于数组的大小,数组必须包含一个终止 NUL 字符.

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

#include int main(){char const * test = "一二三四";printf("测试:%3s\n",测试);printf("测试:%3s\n",测试+4);printf("测试:%5s\n",测试+8);printf("测试:%4s\n",测试+14);返回0;}

输出

<前>测试:一二三四测试:二三四测试:三四测试:四

当我认为我应该得到

<前>测试:一测试:两个测试:三测试:四

我做错了什么,还是手册页只是在骗我?

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

解决方案

您不是在设置 精度,而是在设置 字段宽度.精度 在格式规范中总是以 . 开头.

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

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.

解决方案

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);

这篇关于printf 似乎忽略了字符串精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆