printf \t option

后端 未结 2 1869
萌比男神i
萌比男神i 2020-12-16 10:38

When you are printing a tab character to the standard output using printf in C, it outputs some space which is apparently 4 characters in length.



        
相关标签:
2条回答
  • 2020-12-16 11:03

    That's something controlled by your terminal, not by printf.

    printf simply sends a \t to the output stream (which can be a tty, a file etc), it doesn't send a number of spaces.

    0 讨论(0)
  • 2020-12-16 11:06

    A tab is a tab. How many spaces it consumes is a display issue, and depends on the settings of your shell.

    If you want to control the width of your data, then you could use the width sub-specifiers in the printf format string. Eg. :

    printf("%5d", 2);
    

    It's not a complete solution (if the value is longer than 5 characters, it will not be truncated), but might be ok for your needs.

    If you want complete control, you'll probably have to implement it yourself.

    0 讨论(0)
提交回复
热议问题