Home > database >  What it means the 6.3.1.8 section of the C standard?
What it means the 6.3.1.8 section of the C standard?

Time:12-09

the 6.3.1.8 section of the C standard says the next: . . .

Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:

. . .

What I don't understand is the part that says "Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise"

This means like, for example, if I have this line of code:

f=x y;

where x is a double, y is a complex float and f is a long double

So the statement of the C Standard says that, in this example, the corresponding real type of the result is double? And if that's right, so x keeps being a double, and y is converted to a complex double right? And finally the result is converted to long double and assigned to f right?

If I wrong can you give an example of what this statement means?

CodePudding user response:

The conversion rules actually apply to each individual operator, not the expression as a whole.

First you have x y, where x is double and y is float complex. The common real type is double, so the value of x still has type double while the value of y is converted to double complex, so type of the resulting expression x y is double complex.

Then you have the = operator, whole left operand f has type long double and right operand x y has type double complex. This operator is not subject to the usual arithmetic conversions. Section 6.5.15.1p2 states:

In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.

So the value of x y is converted from double complex to long double as per this rule and assigned to f. If the type of f was float, then you would have a conversion from double complex to float.

  • Related