1

Is there any other promotion besides promotion of 0 from int to double in first line?

double d=-1./0;   
unsigned int *pd = (unsigned int *)&d; 
printf("1:%08x\n",*++pd);   
printf("2:%08x",*--pd); 
4

2 回答 2

2

.. besides promotion of 0 from int to double ..

Well, the standard doesn't use the term "promotion" for this. The standard uses the term "Usual arithmetic conversions".

The term "promotion" in the standard is related to "integer promotion" and tells how different integer types are converted to a common integer type before applying the operator used..

BTW: Making a unsigned int pointer point to a double object and dereferencing it, is a violation of the strict aliasing rule.

Edit based on comment from Ian Abbott:

The term "promotion" is also used for "default argument promotions", which includes promotion of float to double for the variable arguments of a function or the arguments of a function without a prototype.

于 2020-02-04T10:26:43.447 回答
0

除了在第一行将 0 从 int 提升到 double 之外,还有其他促销活动吗?

不。

  • 1.or1.0是一个类型的常数double
  • 一元-不会改变操作数的类型,因为它不是一个小整数类型,而是一个double.
  • 0is 类型int并提升double通常算术转换的一部分。
  • /has type的结果double。这与 variable 类型相同d,因此不会发生左值转换

此处的详细信息:隐式类型提升规则


与您的问题无关,取消引用double d作为整数类型会调用未定义的行为错误:它违反了严格的别名规则。在指向类型的末尾进行指针算术也是未定义的行为。

于 2020-02-04T15:16:00.973 回答