admin管理员组文章数量:1123600
I read the c documentation but I couldn't understand it. Could you please answer my questions(Maybe my English is not good.) I have two codes and two question.
Question about first code: Has an integer promotion been made?
I mean, y-->int(integer promotion) --> Double ?
or
y-->Double?(Converted directly to Double. Integer promotions are not made.) ?
which one is true?
//First code
#include <stdio.h>
int main() {
double x = 11.153;
short y = 123;
x - y;
return 0;
}
Question about second code: Has an integer promotion been made?
I mean, x-->int(integer promotion)-->long.
or
x --> long (Converted directly to long. Integer promotions are not made.) ?
which one?
//Second Code
#include <stdio.h>
int main() {
short x = 15;
long y = 12;
x - y;
return 0;
}
exact technical answers
I read the c documentation but I couldn't understand it. Could you please answer my questions(Maybe my English is not good.) I have two codes and two question.
Question about first code: Has an integer promotion been made?
I mean, y-->int(integer promotion) --> Double ?
or
y-->Double?(Converted directly to Double. Integer promotions are not made.) ?
which one is true?
//First code
#include <stdio.h>
int main() {
double x = 11.153;
short y = 123;
x - y;
return 0;
}
Question about second code: Has an integer promotion been made?
I mean, x-->int(integer promotion)-->long.
or
x --> long (Converted directly to long. Integer promotions are not made.) ?
which one?
//Second Code
#include <stdio.h>
int main() {
short x = 15;
long y = 12;
x - y;
return 0;
}
exact technical answers
Share Improve this question asked 21 hours ago İlker Deveciİlker Deveci 694 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 4Start by reading Implicit type promotion rules regarding integer promotion and the usual arithmetic conversions.
Integer promotion often happens independently of the usual arithmetic conversions, but if the usual arithmetic conversions apply, they may involve integer promotion as one of the steps.
This is operator-specific. Your specific case is the binary -
operator, which is part of the additive operators. C23 6.5.7 then says:
If both operands have arithmetic type, the usual arithmetic conversions are performed on them.
Now read the quoted steps of the usual arithmetic conversions in the link I posted.
- Floating point types are checked for first, before integer types.
- If either operand is of floating point type and the other an integer type, a conversion to the floating point type will happen before the step mentioning integer promotion.
- Similarly, if neither operand is floating point and both are integer types, we arrive at the step "Otherwise, the integer promotions are performed on both operands." Which only applies for small integer types, not for
long
etc.
C 2024 (the 2024 edition of the C standard) specifies the binary -
operator in clause 6.57. Part of that says:
If both operands have arithmetic type, the usual arithmetic conversions are performed on them.
6.3.2.8 specifies the usual arithmetic conversions with a list of 13 steps, so I will not detail all of them here. The first four apply only when there is a decimal floating-point type, and the fifth only when there is a long double
. For your case of double
minus short
, the sixth step applies:
Otherwise, if the corresponding real type of either operand is
double
, the other operand is converted, without change of type domain, to a type whose corresponding real type isdouble
.
The real type of double
is double
,1 so this condition is met, and the short
is converted to double
. Then the usual arithmetic conversions are done, and the subtraction is performed using double
and double
.
For your case of short
minus long
, that sixth step does not apply, and neither does the seventh (about float
). The next step says:
Otherwise, if any of the two types is an enumeration, it is converted to its underlying type. Then, the integer promotions are performed on both operands.
Neither short
nor long
is an enumeration, so that does not apply. Then the integer promotions are performed. 6.3.2.1 specifies the integer promotions. First, the integer promotions apply only to:
An object or expression with an integer type (other than
int
orunsigned int
) whose conversion rank is less than or equal to the rank ofint
andunsigned int
.A bit-field of type
bool
,int
,signed int
, orunsigned int
.
Rank is specified in the same clause 12 rules. It is basically an ordering of integer types from narrower to wider. long
has greater rank than int
and is not a bit-field, so it is not affected by the integer promotions. short
has lesser rank than int
, so the integer promotions apply to it. The integer promotions are, still in 6.3.2.1:
The value from a bit-field of a bit-precise integer type is converted to the corresponding bit-precise integer type. If the original type is not a bit-precise integer type (6.2.5): if an
int
can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to anint
; otherwise, it is converted to anunsigned int
. These are called the integer promotions. All other types are unchanged by the integer promotions.
short
is not a bit-field, and an int
can represent all values of short
, so short
is converted to int
.
Now the integer promotions have left us with a long
and an int
to subtract, and we return to the usual arithmetic conversions.
The next step applies only if the operands are the same type. After that, we have:
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
long
and int
are both signed integer types, so the lesser-ranked int
is converted to the greater-ranked long
. This completes the usual arithmetic conversions, and the subtraction is performed using long
and long
.
Footnote
1 “Real type” and “type domain” refer to complex number types (as in mathematical complex numbers, with real and imaginary parts). “Real type” means the floating-point type that is used for complex numbers. So the real type of complex double
is double
. The real type of double
is double
.
本文标签: type conversionInteger Promotion in CStack Overflow
版权声明:本文标题:type conversion - Integer Promotion in C - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736579677a1944933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
short
andlong
…”: It does not depend on the sizes for this case. The integer promotions convertshort
toint
even if they are the same size, and the usual arithmetic conversions convert ashort
-long
pair tolong
-long
even ifshort
andlong
are the same size. – Eric Postpischil Commented 19 hours ago