godMode
godMode

Reputation: 381

How do I append blank spaces to the end of a string using printf?

How do I append blank spaces to the end of a string using printf?

I can't find any examples where spaces are appended to the right. A similar question I found use printf to add spaces to the left of the string instead.

Upvotes: 6

Views: 9323

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Use negative numbers to left-align (i.e. "pad" to the right).

#include <stdio.h>
int main() {
    const char *s = "hello";
    printf("%30sworld\n", s);
    printf("%-30sworld\n", s);
}

This prints

                         helloworld
hello                         world

Upvotes: 6

Related Questions