January 29th, 2005, 06:18 PM
a variable width precision using printf
I have a situation where I want to set a field width to be the size of the greatest input (length of char*).
I know in c++ you can use setw manipulators and there is the %20s (or other number) for printf, but what if you dont know the width of the output until after the read, can you use a variable in the printf statement? Or am I confined to guessing the length?
In case it helps, I am getting the length from the return value of getline.
January 29th, 2005, 06:30 PM
There is * for field width or precision.
const char text = "Hello world";
for ( i = 1; i < 12; ++i )
printf("\"%.*s\"\n", i, text);
/* my output
January 29th, 2005, 06:32 PM
WOW!!! That is GRRRREAT!!!
I have been writing code since the mid-70's (including lots of different assembly-languages), C since the early 80's, and yet I am always learning something new.
This trick with specifying a width for a printf field using a variable is something I have never heard or seen before, but is exactly what I need for my current problem.
THANKS A MILLION! WOW!!!!
:D :o :D