Ç - 用printf()来获得在输出同等长度如何格式化字符串? [英] C - How to format strings using printf() to get equal length in the output?

查看:139
本文介绍了Ç - 用printf()来获得在输出同等长度如何格式化字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个功能,一是产生的消息像启动初始化... 和另一个用于检查返回codeS和输出确定警告错误。然而,所产生的输出是不同长度的

 启动初始化...好吧。
检查初始化脚本...好吧。

我怎样才能得到的东西是这样的:

 启动初始化...好吧。
检查初始化脚本...好吧。


解决方案

您可以在字符串字段,例如指定宽度

 的printf(% -  20年代,初始化...);

无论是印有该领域

然后将填充空白以您指定的宽度。

- 左证明在该领域的文本

I have two functions, one which produces messages like Starting initialization... and another which checks return codes and outputs "Ok", "Warning" or "Error". However, the output that is produced is of the different length:

Starting initialization...Ok.
Checking init scripts...Ok.

How can I get something like this:

Starting initialization...       Ok.
Checking init scripts...         Ok.

解决方案

You can specify width on string fields, e.g.

printf("%-20s", "initialization...");

and then whatever's printed with that field will be blank-padded to the width you indicate.

The - left-justifies your text in that field.

这篇关于Ç - 用printf()来获得在输出同等长度如何格式化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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