printf \t 选项 [英] printf \t option

查看:78
本文介绍了printf \t 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在 C 中使用 printf 将制表符打印到标准输出时,它会输出一些显然长度为 4 个字符的空格.

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.

printf("\t");

在上述情况下,有什么方法可以控制标签宽度?任何帮助或建议表示赞赏.

Is there any way by which I can control the tab width in the above case? Any help or suggestion is appreciated.

推荐答案

选项卡就是选项卡.它占用多少空间是一个显示问题,取决于您的外壳设置.

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

如果您想控制数据的宽度,则可以在 printf 格式字符串中使用 width 子说明符.例如.:

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

这不是一个完整的解决方案(如果值超过 5 个字符,则不会被截断),但可能可以满足您的需求.

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.

这篇关于printf \t 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆