comparison - What are the general rules for comparing different data types in C? -
comparison - What are the general rules for comparing different data types in C? -
lets have next scenarios:
int = 10; short s = 5; if (s == i){ stuff... } else if (s < i) { stuff... }
when c comparing convert smaller info type, in case short int or convert info type on right info type on left? in case int short?
this governed usual arithmetic conversions. simple cases, general rule of thumb type "less" precision converted match type "more" precision, gets complex 1 time start mixing signed
, unsigned
.
in c99, described section 6.3.1.8, include here convenience:
first, if corresponding real type of either operand long double
, other operand converted, without alter of type domain, type corresponding real type long double
.
otherwise, if corresponding real type of either operand double
, other operand converted, without alter of type domain, type corresponding real type double
.
otherwise, if corresponding real type of either operand float
, other operand converted, without alter of type domain, type corresponding real type float
.
otherwise, integer promotions performed on both operands. next rules applied promoted operands:
if both operands have same type, no farther conversion needed. otherwise, if both operands have signed integer types or both have unsigned integer types, operand type of lesser integer conversion rank converted type of operand greater rank. otherwise, if operand has unsigned integer type has rank greater or equal rank of type of other operand, operand signed integer type converted type of operand unsigned integer type. otherwise, if type of operand signed integer type can represent of values of type of operand unsigned integer type, operand unsigned integer type converted type of operand signed integer type. otherwise, both operands converted unsigned integer type corresponding type of operand signed integer type.i've highlighted part applies particular example.
the concept of integer conversion rank defined in section 6.3.1.1, , describes might expect (that types less precision have lower rank types more precision).
c comparison types
Comments
Post a Comment