C语言 printf \t 选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6646039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printf \t option
提问by liv2hak
When you are printing a tab character to the standard output using printfin C, it outputs some space which is apparently 4 characters in length.
当您使用printfC将制表符打印到标准输出时,它会输出一些显然长度为 4 个字符的空格。
printf("\t");
Is there any way by which I can control the tab width in the above case? Any help or suggestion is appreciated.
在上述情况下,有什么方法可以控制标签宽度?任何帮助或建议表示赞赏。
回答by cnicutar
That's something controlled by your terminal, not by printf.
这是由您的终端控制的,而不是由printf.
printfsimply sends a \tto the output stream (which can be a tty, a file etc), it doesn't send a number of spaces.
printf只需将 a 发送\t到输出流(可以是 tty、文件等),它不会发送许多空格。
回答by Sander De Dycker
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 widthsub-specifiers in the printfformat string. Eg. :
如果要控制数据的宽度,则可以使用格式字符串中的width子说明printf符。例如。:
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.
这不是一个完整的解决方案(如果值超过 5 个字符,则不会被截断),但可能可以满足您的需求。
If you want complete control, you'll probably have to implement it yourself.
如果您想要完全控制,您可能必须自己实现。

