1

Is it possible to form a format string having nested format character? That is, if I want to specify field width for a long integer with some width which in turn specified by an integer. See the illustration below

printf("%%dld", integer, long_integer);

Of course, it's a bad idea, but I want to know if there is any other way to perform something similar to this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

2

See the (POSIX) specification for printf(). It pays you to read it, and re-read it, and re-re-read it, each year or so after you done it the first dozen or so times. It is big, and complex, and you'll usually find something new each time — at least, that's how it works for me.

You can use:

printf("%*ld", integer, long_integer);

The * consumes an int value and allows you to specify the width of the field.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278