解码 C 中的 printf 语句(Printf Primer)

发布于 2024-07-04 03:40:18 字数 428 浏览 5 评论 0原文

我正在努力将 1998 年的一些旧代码引入到 21 世纪。 该过程的第一步是将 printf 语句转换为 QString 变量。 不管我回顾多少次 printf,我总是会忘记一件事或另一件事。 因此,为了好玩,让我们一起解码它,在这个过程中为 Stackoverflow 创建第一个小'printf prime'

在代码中,我遇到了这个小问题,

printf("%4u\t%016.1f\t%04X\t%02X\t%1c\t%1c\t%4s", a, b, c, d, e, f, g);

变量a、b、c、d、e、f、g将如何格式化?

I'm working on bringing some old code from 1998 up to the 21st century. One of the first steps in the process is converting the printf statements to QString variables. No matter how many times I look back at printf though, I always end up forgetting one thing or the other. So, for fun, let's decode it together, for ole' times sake and in the process create the first little 'printf primer' for Stackoverflow.

In the code, I came across this little gem,

printf("%4u\t%016.1f\t%04X\t%02X\t%1c\t%1c\t%4s", a, b, c, d, e, f, g);

How will the variables a, b, c, d, e, f, g be formatted?

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

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

发布评论

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

评论(6

盗琴音 2024-07-11 03:40:19

@jj33,你在这两方面都是绝对正确的。

#include <stdio.h>

int main(int argc, char *argv[]) {
    char *s = "Hello, World";
    char *s2 = "he";

    printf("4s: '%4s'\n", s);
    printf(".4s: '%.4s'\n", s);
    printf("4s2: '%4s'\n", s2);
    printf(".4s2: '%.4s'\n", s2);

    return 0;
}

$ gcc -o foo foo.c
$ ./foo
4s: 'Hello, World'
.4s: 'Hell'
4s2: '  he'
.4s2: 'he'

接得好!

@jj33, you're absolutely right, on both counts.

#include <stdio.h>

int main(int argc, char *argv[]) {
    char *s = "Hello, World";
    char *s2 = "he";

    printf("4s: '%4s'\n", s);
    printf(".4s: '%.4s'\n", s);
    printf("4s2: '%4s'\n", s2);
    printf(".4s2: '%.4s'\n", s2);

    return 0;
}

$ gcc -o foo foo.c
$ ./foo
4s: 'Hello, World'
.4s: 'Hell'
4s2: '  he'
.4s2: 'he'

Good catch!

泛滥成性 2024-07-11 03:40:19

您真正需要的是一个工具,它采用 printf() 语句中的格式字符串并将它们转换为等效的基于 QString 的函数调用。
有人想将他的免费软件捐赠时间用于开发这样的工具吗?

保存此类工具源代码的自由软件托管服务 URL 的占位符

What you really need is a tool which takes the format strings in printf() statements and converts them into equivalent QString based function calls.
Does anyone want to spend his Free Software Donation Time on developing such a tool?

Placeholder for URL to a Free Software hosting service holding the source code of such a tool

牵你的手,一向走下去 2024-07-11 03:40:19

A。 十进制,四位有效数字

b. 不确定

C. 十六进制,至少 4 个字符

d. 也是十六进制,至少 2 个字符

e。 1 个字符

f。 字符串,最少 4 个

a. decimal, four significant digits

b. Not sure

c. hex, minimum 4 characters

d. Also hex, minimum 2 characters

e. 1 character

f. String of characters, minimum 4

仅冇旳回忆 2024-07-11 03:40:19

@Jason Day,我认为最后%中的4如果字符少于 4 个,则 4s 很重要。 如果超过 4 个,您是对的,%4s 和 %s 将相同,但 g 中的字符少于 4 个时,%s 将左对齐,而 %4s 在 4 个字符字段中将右对齐。

b 实际上是整个字段的至少 16 个字符,包括小数点和小数点后的个位数,我认为(总共 16 个字符 vs 总共 18 个字符)

@Jason Day, I think the 4 in the last %4s is significant if there are fewer than 4 characters. If there are more than 4 you are right, %4s and %s would be the same, but with fewer than 4 chars in g %s would be left justified and %4s would be right-justified in a 4 char field.

b is actually minimum 16 chars for the whole field, including the decimal and the single digit after the decimal I think (16 total chars vs 18 total chars)

老娘不死你永远是小三 2024-07-11 03:40:19

这是我的 printf 底漆:
http://www.pixelbeat.org/programming/gcc/format_specs.html

我总是使用 -Wall 和 gcc 进行编译
将警告所提供的任何不匹配
printf 格式和变量。

Here's my printf primer:
http://www.pixelbeat.org/programming/gcc/format_specs.html

I always compile with -Wall with gcc which
will warn about any mismatches between the supplied
printf formats and variables.

帅气称霸 2024-07-11 03:40:18

丹尼基本上是对的。

A。 无符号十进制,最少 4 个字符,空格填充
b. 浮点数,小数点前至少 16 位(0 填充),小数点后 1 位
C。 十六进制,最少 4 个字符,0 填充,字母以大写形式打印
d. 与上面相同,但至少 2 个字符
e. e 假定为 int,转换为 unsigned char 并打印
F。 与 e 相同
G。 这可能是一个拼写错误,4 没有任何效果。 如果是“%.4s”,则最多会打印字符串中的 4 个字符。 有趣的是,在这种情况下,字符串不需要以 null 结尾。

编辑: jj33 指出上面 b 和 g 中的 2 个错误 此处

Danny is mostly right.

a. unsigned decimal, minimum 4 characters, space padded
b. floating point, minimum 16 digits before the decimal (0 padded), 1 digit after the decimal
c. hex, minimum 4 characters, 0 padded, letters are printed in upper case
d. same as above, but minimum 2 characters
e. e is assumed to be an int, converted to an unsigned char and printed
f. same as e
g. This is likely a typo, the 4 has no effect. If it were "%.4s", then a maximum of 4 characters from the string would be printed. It is interesting to note that in this case, the string does not need to be null terminated.

Edit: jj33 points out 2 errors in b and g above here.

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