Type argument twice

I want to pass one additional argument printfand print it twice, for example

printf("%s%s","somestring");       // prints somestringsomestring

Is there any way to do this?

+4
source share
1 answer

If you are on Linux or some other system such as UNIX, you can use $to specify the argument number:

printf("%1$s%1$s\n", "hello");

In this example, 1$means "use the first argument". We also use this syntax several times, so this argument can be used once.

Linux man page forprintf gives more details:

( ) . '*' ( , ). , , , "% m $" '%' "m $" '', m , 1. ,

printf("%*d", width, num);

printf("%2$*1$d", width, num);

. . C99 , '$', Single UNIX. '$' , , , "%%", . , '$'; , 1 3, 2 - .

+9

Source: https://habr.com/ru/post/1677952/


All Articles