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

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

问题描述

我见过C语言编写的源$ C ​​$ 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).

所以,当你运行下面的code:

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 在标准输出。

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

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

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