We all know in C-based languages, printf("%11d", some_int); means right align within an 11 character field, but what if I want to replace this constant 11 here with a dynamic variable, what am I gonna do ?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
You can use the
*
character to specify the field width in its own argument:use linux command: "man 3 printf" to get more information. One way to do this is
where width is precision and num is argument to print. Other way equivalent to above one is
More generally this is written as "*m$d", where m is int and argument number.
One way of doing this is to use an snprintf-equivalent on a character array buffer, in order to create your printf format string. (Be sure to check for buffer overflows, of course.)
You are going to read the
printf(3)
man page and come across the following: