Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 580
godMode Avatar asked Mar 12 '12 23:03

godMode


1 Answers

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
like image 135
Sergey Kalinichenko Avatar answered Nov 17 '22 06:11

Sergey Kalinichenko