在 gcc 中,如何用前导 0 填充数字字符串?

发布于 2024-10-11 03:55:38 字数 425 浏览 1 评论 0原文

我有以下代码

#include <stdio.h>
#include <stdlib.h>

char UPC[]="123456789ABC";

main()
{
  int rc=0;

  printf("%016s\n",UPC);

  exit(rc);
}

在 AIX 上使用 xlC 编译器此代码打印出前导 0

0000123456789ABC

在 Sles 11 上它使用 gcc 版本 4.3.2 [gcc-4_3-branch revision 141291] 打印前导空格

123456789ABC

是否有一些我可以用于字符串的格式说明符打印前导 0 吗? 我知道它适用于数字类型。

I have the following code

#include <stdio.h>
#include <stdlib.h>

char UPC[]="123456789ABC";

main()
{
  int rc=0;

  printf("%016s\n",UPC);

  exit(rc);
}

On AIX using the xlC compiler this code prints out with leading 0's

0000123456789ABC

On Sles 11 it prints leading spaces using gcc version 4.3.2 [gcc-4_3-branch revision 141291]

123456789ABC

Is there some format specifier I could use for strings to print out with leading 0's?
I know that it works for numeric types.

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

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

发布评论

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

评论(7

夜司空 2024-10-18 03:55:38
printf("%.*d%s", (int)(w-strlen(s)), 0, s);
printf("%.*d%s", (int)(w-strlen(s)), 0, s);
酒浓于脸红 2024-10-18 03:55:38

此行为未定义,特定于实现(glibc,而不是 gcc)。在我看来,依赖它是不好的做法。

如果您确定您的字符串是数字(此处为十六进制),您可以编写:

 printf("%016llX\n",strtoll(UPC,NULL,16));

但要注意错误和溢出。

由原始发布者编辑:

对于十进制数字,请使用以下内容:

printf("%016llu\n",strtoll(UPC2,NULL,10));

This behaviour is undefined, implementation specific (glibc, rather than gcc). It's bad practice to rely on it, IMO.

If you know for certain that your string is numeric (hexadecimal here), you could write:

 printf("%016llX\n",strtoll(UPC,NULL,16));

But be aware of errors and overflows.

Edit by original poster:

For decimal numbers use the following:

printf("%016llu\n",strtoll(UPC2,NULL,10));
誰認得朕 2024-10-18 03:55:38

printf 而言,0 标志对 s 转换说明符的影响是未定义的。您是否仅限于 printf

[编辑]
如果您愿意,这里有一个替代方案(大多数错误检查缺失..):

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

int main(int argc, char **argv)
{
    char    UPC[] = "1234567890ABCDEF",
        out[sizeof UPC + 12]; /* substitute size for whatever you wish here, just make sure the original string plus NUL fits */
    if(sizeof out <= sizeof UPC) {
        return -1; /* you bad, bad man */
    }

    memset(out, '0', sizeof out);
    memcpy(out + sizeof out - sizeof UPC - 1, UPC, sizeof UPC);

    printf("%s\n", out);
    return 0;
}

As far as printf is concerned, the effect of the 0 flag on the s conversion specifier is undefined. Are you restricted to printf?

[edit]
Here's an alternative, if you'd like (most error checks missing..):

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

int main(int argc, char **argv)
{
    char    UPC[] = "1234567890ABCDEF",
        out[sizeof UPC + 12]; /* substitute size for whatever you wish here, just make sure the original string plus NUL fits */
    if(sizeof out <= sizeof UPC) {
        return -1; /* you bad, bad man */
    }

    memset(out, '0', sizeof out);
    memcpy(out + sizeof out - sizeof UPC - 1, UPC, sizeof UPC);

    printf("%s\n", out);
    return 0;
}
明月松间行 2024-10-18 03:55:38

%0*s 不是标准的。不过你可以自己做填充:

char buf[16];
memset(buf, '0', sizeof(buf));
printf("%.*s%s", sizeof(buf) - strlen(UPC), buf, UPC);

%0*s isn't standard. You can do your own padding though:

char buf[16];
memset(buf, '0', sizeof(buf));
printf("%.*s%s", sizeof(buf) - strlen(UPC), buf, UPC);
枕梦 2024-10-18 03:55:38

根据 printf 手册页,0 标志对于字符串参数具有未定义的行为。您需要编写自己的代码来填充正确数量的“0”字符。

According to the printf man page, the 0 flag has undefined behavior for string arguments. You'd need to write your own code to pad out the right number of '0' characters.

瀞厅☆埖开 2024-10-18 03:55:38

简短的回答是否定的。

您的代码在编译时将导致:“警告:‘0’标志与‘%s’一起使用”

printf 的手册页列出了可在‘0’标志后使用的格式说明符,并且它们都是数字。

但是,您可以创建一个包含适当数量空格的字符串,并将其打印在 UPC 之前。

The short answer is no.

Your code, when compiled, will result in : "warning: '0' flag used with ‘%s’"

The man page for printf lists the format specifiers that may be used after a '0' flag and they are all numeric.

You could, however, create a string with the appropriate number of spaces and print that ahead of your UPC.

り繁华旳梦境 2024-10-18 03:55:38
 char spbuf[30];
 sprintf(spbuf, "%%.%ds%%s\n", 16 - strlen(UPC));
 printf(spbuf,"0000000000000000",UPC); /* 16 '0' characters */
 char spbuf[30];
 sprintf(spbuf, "%%.%ds%%s\n", 16 - strlen(UPC));
 printf(spbuf,"0000000000000000",UPC); /* 16 '0' characters */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文