157

How can I pad a string with spaces on the left when using printf?

For example, I want to print "Hello" with 40 spaces preceding it.

Also, the string I want to print consists of multiple lines. Do I need to print each line separately?

EDIT: Just to be clear, I want exactly 40 spaces printed before every line.

titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133

4 Answers4

176

If you want the word "Hello" to print in a column that's 40 characters wide, with spaces padding the left, use the following.

char *ptr = "Hello";
printf("%40s\n", ptr);

That will give you 35 spaces, then the word "Hello". This is how you format stuff when you know how wide you want the column, but the data changes (well, it's one way you can do it).

If you know you want exactly 40 spaces then some text, save the 40 spaces in a constant and print them. If you need to print multiple lines, either use multiple printf statements like the one above, or do it in a loop, changing the value of ptr each time.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 4
    dunno too much about format flags. but i suspect printf("%40s%s\n", "", ptr); can be used to pad it with 40 spaces too? – Johannes Schaub - litb Nov 16 '08 at 04:06
  • Yes, that would always give you 40 spaces before the contents of the pointer. That's a nice solution, but if you have to do it a lot I think a constant with 40 spaces would be faster. I don't know if the compiler is able to optimize printf formats. – Bill the Lizard Nov 16 '08 at 04:13
  • 48
    Also, if you have an int variable 'n' that contains the number of spaces to include, you can use: printf("%*s%s\n", n, "", ptr); to get a variable number of spaces. – Jonathan Leffler Nov 16 '08 at 04:37
  • And, to address the last part of the Q: yes, if you want each line of the data to be printed with 40 leading spaces, then you do need to segment the data so that each line is printed separately. – Jonathan Leffler Nov 16 '08 at 04:38
  • 4
    @Jonathan Leffler: of course the compiler can optimise printf formats. For instance, GCC optimises printf("%s\n", foo) and replaces it with puts(foo). – sam hocevar Jan 10 '10 at 01:16
98

I use this function to indent my output (for example to print a tree structure). The indent is the number of spaces before the string.

void print_with_indent(int indent, char * string)
{
    printf("%*s%s", indent, "", string);
}
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
jk_
  • 5,448
  • 5
  • 25
  • 23
  • 9
    I like your use of string literal concatenation to keep indentation separate from the rest of the printf format. One suggestion: use an empty string instead of a single space for the indent string. That way indent == 0 will work as expected. (i.e., `printf(..., indent, "", ...)`) – ɲeuroburɳ Aug 04 '15 at 15:36
  • 1
    Note, Ray Hulha has removed the string literal concatenation, evidently in order to fix a "compiler issue". – mwfearnley Oct 01 '17 at 14:03
62
int space = 40;
printf("%*s", space, "Hello");

This statement will reserve a row of 40 characters, print string at the end of the row (removing extra spaces such that the total row length is constant at 40). Same can be used for characters and integers as follows:

printf("%*d", space, 10);
printf("%*c", space, 'x');

This method using a parameter to determine spaces is useful where a variable number of spaces is required. These statements will still work with integer literals as follows:

printf("%*d", 10, 10);
printf("%*c", 20, 'x');
printf("%*s", 30, "Hello");

Hope this helps someone like me in future.

joe pelletier
  • 390
  • 2
  • 12
Rece Foc
  • 621
  • 5
  • 2
27

If you want exactly 40 spaces before the string then you should just do:

printf("                                        %s\n", myStr );

If that is too dirty, you can do (but it will be slower than manually typing the 40 spaces): printf("%40s%s", "", myStr );

If you want the string to be lined up at column 40 (that is, have up to 39 spaces proceeding it such that the right most character is in column 40) then do this: printf("%40s", myStr);

You can also put "up to" 40 spaces AfTER the string by doing: printf("%-40s", myStr);

SoapBox
  • 20,457
  • 3
  • 51
  • 87