如何更好地格式化printf语句,所以事情总是排队 [英] How to format printf statement better so things always line up

查看:198
本文介绍了如何更好地格式化printf语句,所以事情总是排队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  printf(name:%s\t
args :%s\t
value%d\t
arraysize%d\t
scope%d\\\

sp- >名称,
sp-> args,
sp->值,
sp->数组大小,
sp->范围);

它位于for循环内部,因此它为指针列表打印多行。

问题是,如果某些打印出来的东西长或短,就会导致排列不整齐。

解决方案

每个转换说明符都可以赋予一个字段宽度,转换将使用的字符。还有其他的标志和精度可以用来控制输出(例如用%s 转换精度项说明将使用多少字符最大值) p>

  printf(name:%20.20s\t
args:%10.10s\t
value%6d\t
arraysize%6d\t
scope%6d\\\

sp-> name,
sp - > args,
sp->值,
sp->数组大小,
sp->范围);


I have this printf statement:

 printf("name: %s\t"
        "args: %s\t"
        "value %d\t"
        "arraysize %d\t"
        "scope %d\n",
         sp->name,
         sp->args,
         sp->value,
         sp->arraysize,
         sp->scope);

It's inside a for loop, so it's printing multiple lines for a list of pointers.

The problem is that if some of the things that are printed are longer or shorter, it causes things to not line up. How do I get it to always line up?

解决方案

Each conversion specifier can be given a field width which give the minimum number of characters that conversion will use. There are other flags and precision that can be used to control the output (for example with the %s conversion the precision item says how many characters maximum will be used).

printf("name: %20.20s\t"
        "args: %10.10s\t"
        "value %6d\t"
        "arraysize %6d\t"
        "scope %6d\n",
         sp->name,
         sp->args,
         sp->value,
         sp->arraysize,
         sp->scope);

这篇关于如何更好地格式化printf语句,所以事情总是排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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