Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf("%2$*11$s", ...) what is this formation means

Tags:

c++

c

format

printf

it use like in c11/c++?:

printf("%2$*11$s",...)

It is come from a elf file, this file using printf() to operate like mem[4]=mem[2]+mem[1]. You can refer from this https://ctftime.org/task/5042 (it is a reverse CTF question).

To my point, I know the $ is use to specify the position of which var but no reference notice that one block can have two $.

also, I cannot find any func about $ in format except location.

so, I will be grateful if anyone can tell me it is meaning.

image

like image 616
p0iL Avatar asked Jul 30 '21 16:07

p0iL


People also ask

What does %n printf mean?

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.

What is S in printf?

We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character. String name itself the starting address of the string. So, if we give string name it will print the entire string.

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].


1 Answers

n$ is an extension defined in POSIX to select which argument to print. This is called Parameter field.

printf format string - Wikipedia

Actually this question is about using two n$ in one format specifier. Let me examine with small examples...

#include <stdio.h>

int main(void) {
    printf("%1$*3$s\n", "a", "b", 10, 20);
    printf("%1$*4$s\n", "a", "b", 10, 20);
    printf("%2$*3$s\n", "a", "b", 10, 20);
    printf("%2$*4$s\n", "a", "b", 10, 20);
    return 0;
}

Output:

         a
                   a
         b
                   b

It looks like %n$*m$s means "print the n-th argument using the width specified by the m-th argument". The final s has the meaning of s in %s.

like image 101
MikeCAT Avatar answered Sep 30 '22 09:09

MikeCAT