How to format strings using printf() to get equal length in the output
Asked Answered
W

6

125

I have two functions, one which produces messages like Starting initialization... and another which checks return codes and outputs "Ok", "Warning" or "Error". However, the output that is produced is of the different length:

Starting initialization...Ok.
Checking init scripts...Ok.

How can I get something like the following?

Starting initialization...       Ok.
Checking init scripts...         Ok.
Willywillynilly answered 27/11, 2009 at 15:38 Comment(0)
B
205

You can specify a width on string fields, e.g.

printf("%-20s", "initialization...");

And then whatever's printed with that field will be blank-padded to the width you indicate.

The - left-justifies your text in that field.

Bottrop answered 27/11, 2009 at 15:42 Comment(3)
Alas, I'm not clever enough to count that high. However, I trust the asker to be intelligent enough to extrapolate from my example :)Bottrop
what about right justified? There aren't any flags mentioned in K&RSalaried
Right justified is the default. Without a -, your data will be right justified in a field whose width you specified.Bottrop
S
33

Additionally, if you want the flexibility of choosing the width, you can choose between one of the following two formats (with or without truncation):

int width = 30;
// No truncation uses %-*s
printf( "%-*s %s\n", width, "Starting initialization...", "Ok." );
// Output is "Starting initialization...     Ok."

// Truncated to the specified width uses %-.*s
printf( "%-.*s %s\n", width, "Starting initialization...", "Ok." );
// Output is "Starting initialization... Ok."
Shocker answered 5/3, 2015 at 4:49 Comment(0)
H
30

printf allows formatting with width specifiers. For example,

printf( "%-30s %s\n", "Starting initialization...", "Ok." );

You would use a negative width specifier to indicate left-justification because the default is to use right-justification.

Hindi answered 27/11, 2009 at 15:43 Comment(0)
H
16

There's also the %n modifier which can help in certain circumstances. It returns the column on which the string was so far. Example: you want to write several rows that are within the width of the first row like a table.

int width1, width2;
int values[6][2];
printf("|%s%n|%s%n|\n", header1, &width1, header2, &width2);

for(i=0; i<6; i++)
   printf("|%*d|%*d|\n", width1, values[i][0], width2, values[i][1]);

will print two columns of the same width of whatever length the two strings header1 and header2 may have. I don't know if all implementations have the %n, but Solaris and Linux do.

Hooten answered 28/11, 2009 at 19:31 Comment(1)
This solution works, but anyone who takes this and puts it in their code should be aware of the risk of introducing memory vulnerabilities.Switch
B
1

There's also the rather low-tech solution of counting adding spaces by hand to make your messages line up. Nothing prevents you from including a few trailing spaces in your message strings.

Bottrop answered 27/11, 2009 at 15:44 Comment(0)
G
-3

Start with the use of tabs - the \t character modifier. It will advance to a fixed location (columns, terminal lingo).

However, it doesn't help if there are differences of more than the column width (4 characters, if I recall correctly).

To fix that, write your "OK/NOK" stuff using a fixed number of tabs (5? 6?, try it). Then return (\r) without new-lining, and write your message.

Garibald answered 27/11, 2009 at 15:43 Comment(2)
There's always the danger that a string is longer than a tab stop thereby forcing the resulting tab and text to be mis-aligned.Hindi
Not really... first write the tabbed OK/NOK, then /r, now write the phrase, now /n. The phrase could overwrite the OK/NOK. But your way is way better, I didn't know about negative justification.Garibald

© 2022 - 2024 — McMap. All rights reserved.