printf justification of printed values

B

bahadir.balban

Hi,

I checked the C reference manual, but cannot find the right solution
for my printfs.

If I have a print of the format <field: value> such as

printf("Field: %x", val); or
printf("%s%x","Field:",val);

I want the "Field:" string to be padded on its right hand side, such
that, no matter how long the field string is, the value will appear in
the same column. How do I go doing that?

Thanks,
Bahadir
 
S

S.Tobias

printf("Field: %x", val); or
printf("%s%x","Field:",val);
I want the "Field:" string to be padded on its right hand side, such
that, no matter how long the field string is, the value will appear in
the same column. How do I go doing that?

Wouldn't this example do what you want?
printf("%-20s%x", "Field:", val);
 
R

Robert W Hand

Yes this was just what I needed.

You can determine the width dynamically, too, if you cannot estimate a
proper field width at compile time.

printf("%-*s: %*x\n", widthS, field, widthI, value);

The '*' are supplied by widthS and widthI, int objects whose values
are determined programmatically to be a maximal field width for the
two other objects field and value. Spending a little more computer
time to make your output human readable can be well-worth it. HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top