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); 
anastaciu
  • 23,467
  • 7
  • 28
  • 53

2 Answers2

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.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Just to add: 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. – Ian Abbott Feb 04 '20 at 10:37
  • 1
    @IanAbbott I've edited your comment into the answer – Support Ukraine Feb 04 '20 at 11:11
  • Promotion is _part_ of "the usual arithmetic", among other implicit conversion rules. Specifically: "Otherwise, the integer promotions are performed on both operands." – Lundin Feb 04 '20 at 15:10
0

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

No.

  • 1. or 1.0 is a constant of type double.
  • Unary - does not change the type of the operand, since it was not a small integer type but a double.
  • The 0 is of type int and promoted to double as part of the usual arithmetic conversions.
  • The result of the / has type double. This is the same type as variable d, so no lvalue conversion takes place.

Details here: Implicit type promotion rules


Unrelated to your question, de-referencing the double d as an integer type invokes undefined behavior bugs: it's a violation of the strict aliasing rule. Doing pointer arithmetic beyond the end of the pointed-at type is also undefined behavior.

Lundin
  • 195,001
  • 40
  • 254
  • 396