简体   繁体   English

c语言中%d和%*d有什么区别?

[英]What is the difference between %d and %*d in c language?

What is %*d ? %*d什么? I know that %d is used for integers , so I think %*d also must related to integer only?我知道%d用于integers ,所以我认为%*d也必须只与整数有关? What is the purpose of it?它的目的是什么? What does it do?它有什么作用?

int a=10,b=20;
printf("\n%d%d",a,b);
printf("\n%*d%*d",a,b);

Result is结果是

10 20 
1775 1775 

The %*d in a printf allows you to use a variable to control the field width, along the lines of: printf%*d允许您使用变量来控制字段宽度,如下所示:

int wid = 4;
printf ("%*d\n", wid, 42);

which will give you:这会给你:

..42

(with each of those . characters being a space). (每个.字符都是一个空格)。 The * consumes one argument wid and the d consumes the 42 . *消耗一个参数widd消耗42

The form you have, like:您拥有的表格,例如:

printf ("%*d %*d\n", a, b);

is undefined behaviour as per the standard, since you should be providing four arguments after the format string, not two (and good compilers like gcc will tell you about this if you bump up the warning level).根据标准是未定义的行为,因为您应该在格式字符串后提供四个参数,而不是两个(如果您提高警告级别,像gcc这样的优秀编译器会告诉您这一点)。 From C11 7.20.6 Formatted input/output functions :C11 7.20.6 Formatted input/output functions

If there are insufficient arguments for the format, the behavior is undefined.如果格式的参数不足,则行为未定义。

It should be something like:它应该是这样的:

printf ("%*d %*d\n", 4, a, 4, b);

And the reason you're getting the weird output is due to that undefined behaviour.你得到奇怪输出的原因是由于未定义的行为。 This excellent answer shows you the sort of things that can go wrong (and why) when you don't follow the rules, especially pertaining to this situation. 这个出色的答案向您展示了当您不遵守规则时可能会出错的事情(以及原因),尤其是与这种情况有关的事情。

Now I wouldn't expect this to be a misalignment issue since you're using int for all data types but, as with all undefined behaviour, anything can happen.现在我不希望这是一个未对齐的问题,因为您对所有数据类型都使用int ,但是,与所有未定义的行为一样,任何事情都可能发生。

When used with scanf() functions, it means that an integer is parsed, but the result is not stored anywhere.当与 scanf() 函数一起使用时,这意味着解析了一个整数,但结果不会存储在任何地方。

When used with printf() functions, it means the width argument is specified by the next format argument.当与 printf() 函数一起使用时,这意味着宽度参数由下一个格式参数指定。

* 用作指示宽度作为 printf 的参数传递

in "%*d", the first argument is defined as the total width of the output, the second argument is taken as normal integer.在“%*d”中,第一个参数被定义为输出的总宽度,第二个参数被视为正常整数。 for the below program对于以下程序

int x=6,p=10;
printf("%*d",x,p);
output: "    10"

the first argument ta passed for *, that defines the total width of the output... in this case, width is passed as 6. so the length of the entire output will be 5. now to the number 10, two places are required (1 and 0, total 2).传递给 * 的第一个参数 ta 定义了输出的总宽度...在这种情况下,宽度被传递为 6。因此整个输出的长度将为 5。现在到数字 10,需要两个位置(1 和 0,共 2 个)。 so remaining 5-2=3 empty string or '\\0' or NULL character will be concatenated before the actual output所以剩余的 5-2=3 空字符串或 '\\0' 或 NULL 字符将在实际输出之前连接

宽度未在格式字符串中指定,而是作为必须格式化的参数之前的附加整数值参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM