Left-pad printf with spaces

前端 未结 4 1097
深忆病人
深忆病人 2020-11-29 21:20

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

4条回答
  •  情书的邮戳
    2020-11-29 22:05

    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);
    }
    

提交回复
热议问题