如何在printf的%*。* D工作()? [英] How does %*.*d work in printf()?

查看:114
本文介绍了如何在printf的%*。* D工作()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

int main()
{
  printf("%*.*d\n", -6 , 7,20000);
  printf("%*.*d\n", 5, -6, 2000);
  return 0;
}

输出:

0020000
 2000

我不明白怎么做的printf 间preT格式说明 *。 *

而在第一次调用的printf(),并在后来的 7 的覆盖前的 -6 吗?使输出宽度的大小变为7

While in the first call to printf(),does the later 7 override the former -6? So that the size of output width turns to 7?

推荐答案

的参数, * 是的字段宽度的和参数的 * precision

The argument to the * before the . is the field width and the argument to the * after the . is the precision.

场宽度是,这将是作为转换的结果输出的最小字节数;输出将被填充(默认情况下,左边用空格,但左补零,右空间填充也是选项,由标志控制),如果少字节将产生。负参数传递给 * 的宽度PTED为与相应正值间$ P $ - 标志,该填充向右移动(即左证明了场)。

Field widths are the minimum number of bytes that will be output as a result of the conversion; the output will be padded (by default, on the left with spaces, but left zero padding and right space padding are also options, controlled by flags) if fewer bytes would be produced. A negative argument to the * for width is interpreted as the corresponding positive value with the - flag, which moves the padding to the right (i.e. left-justifies the field).

precision 的另一方面具有根据正在执行的转换而变化的含义。因为如果没有precision已在所有指定的负precisions进行处理。对于整数,它是的数字的待产(而不是总产出)的最小数目;如果较少的位数会产生,零被添加到左边。当值为0正在生产的0结果在没有数字的显式precision(而不是单个 0 正在制作)。为弦,precision <青霉>限制的输出的字节数,截断串(并允许较长的,非空终止,输入数组)如果需要的话。对于浮点说明符,$​​ P $ pcision控制的印刷名额,无论是小数点后(对于%F )或重要性的总名额(对于其它格式)。

Precision on the other hand has a meaning that varies according to the conversion being performed. Negative precisions are treated as if no precision had been specified at all. For integers, it's the minimum number of digits (not total output) to be produced; if fewer digits would be produced, zeros are added to the left. An explicit precision of 0 results in no digits being produced when the value is 0 (instead of a single 0 being produced). For strings, precision limits the number of output bytes, truncating the string (and permitting a longer, non-null-terminated, input array) if necessary. For floating point specifiers, precision controls the number of places printed, either after the radix point (for %f) or the total places of significance (for the other formats).

在你的例子:

printf("%*.*d\n", -6 , 7,20000);

下面的字段是左对齐(填充上右)与6的最小宽度,但该领域最终被更广泛的,无论如何,所以宽度被忽略。 7部队整数输出的precision至少是7位数,所以你最终与 0020000 作为转换后的字段内容,已经超出了宽度。

Here the field is left-aligned (padding on right) with a minimum width of 6, but the field ends up being wider anyway, so the width is ignored. The precision of 7 forces integer output to be at least 7 digits, so you end up with 0020000 as the converted field contents, which already exceeded the width.

在另一个

printf("%*.*d\n", 5, -6, 2000);

字段宽度为5,默认右对齐;填充是在左侧的空间。负precision被忽略,就好像它是未指定,所以经转换的字段的内容是 2000 ,只有4个字节,其中获得填充到5个字节填宽度由单一主导空间的手段。

The field width is 5, with the default right alignment; padding is with spaces on the left. The negative precision is ignored, as if it were not specified, so the converted field contents are 2000, only 4 bytes, which get padded up to 5 bytes to fill the width by means of a single leading space.

这篇关于如何在printf的%*。* D工作()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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