Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(GCC) Dollar sign in printf format string

Tags:

c

format

gcc

printf

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

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

What does it mean?

like image 900
polkovnikov.ph Avatar asked Oct 11 '13 21:10

polkovnikov.ph


People also ask

What is %N in format string?

In C language, %n is a special format specifier. It cause printf() to load the variable pointed by corresponding argument. The loading is done with a value which is equal to the number of characters printed by printf() before the occurrence of %n.

What is %d %f %s in C?

%s refers to a string %d refers to an integer %c refers to a character. Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0].

What is %A in printf?

The %a formatting specifier is new in C99. It prints the floating-point number in hexadecimal form. This is not something you would use to present numbers to users, but it's very handy for under-the-hood/technical use cases. As an example, this code: printf("pi=%a\n", 3.14); prints: pi=0x1.91eb86p+1.

What does %n Do printf?

In C printf(), %n is a special format specifier which instead of printing something causes printf() to load the variable pointed by the corresponding argument with a value equal to the number of characters that have been printed by printf() before the occurrence of %n. The above program prints “geeks for geeks 10”.


2 Answers

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

you'll get 2 3 in standard output.

More info can be found on printf man pages.

like image 67
polkovnikov.ph Avatar answered Oct 19 '22 14:10

polkovnikov.ph


Per the C spec C11dr 7.21.6.1

As part of a print format, the first % in "%2$d %1$d" introduces a directive. This directive may have various flags, width, precision, length modifier and finally a conversion specifier. In this case 2 is a width. The next character $ is neither a precision, length modifier nor conversion specifier. Thus since the conversion specification is invalid,

... the behavior is undefined. C11dr 7.21.6.1 9

The C spec discusses future library directions. Lower case letters may be added in the future and other characters may be used in extensions. Of course $ is not a lower case letter, so that is good for the future. It certainly fits the "other character" role as $ is not even part of the C character set.

In various *nix implementations, $ is used as describe in Linux Programmer's Manual PRINTF(3). The $, along with the preceding integer defines the argument index of the width.

like image 26
chux - Reinstate Monica Avatar answered Oct 19 '22 15:10

chux - Reinstate Monica