(GCC) printf 格式字符串中的美元符号 [英] (GCC) Dollar sign in printf format string

查看:37
本文介绍了(GCC) printf 格式字符串中的美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用 C 编写的源代码中看到了以下行:

I've seen the following line in a source code written in C:

printf("%2$d %1$d", a, b);

什么意思?

推荐答案

这是一个 对添加的语言的扩展通过 POSIX (符合 C11 的行为应如 @chux 的回答中所述).表示法 %2$d 的含义与 %d 相同(输出有符号整数),除了它使用给定的基于 1 的数字格式化参数(在您的情况下,它是第二个参数, b).

It's an extension to the language added by POSIX (C11-compliant behaviour should be as described in an answer by @chux). Notation %2$d means the same as %d (output signed integer), except it formats the parameter with given 1-based number (in your case it's a second parameter, b).

所以,当你运行以下代码时:

So, when you run the following code:

#include <stdio.h>
int main() {
    int a = 3, b = 2;
    printf("%2$d %1$d", a, b);
    return 0;
}

您将在标准输出中获得 2 3.

you'll get 2 3 in standard output.

更多信息可以在 printf 手册页上找到.

More info can be found on printf man pages.

这篇关于(GCC) printf 格式字符串中的美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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