type cast in c compiler -
consider following example mixed mode expression supported: c = + b*10.5 a , b integers , c float. describe how data typecast appropriate format
you need 'usual arithmetic conversions' in text book, or in c standard (§6.3.1.8 in c11 — iso/iec 9899:2011), reads:
6.3.1.8 usual arithmetic conversions
1 many operators expect operands of arithmetic type cause conversions , yield result types in similar way. purpose determine common real type operands , result. specified operands, each operand converted, without change of type domain, type corresponding real type common real type. unless explicitly stated otherwise, common real type corresponding real type of result, type domain type domain of operands if same, , complex otherwise. pattern called usual arithmetic conversions:
first, if corresponding real type of either operand
long double, other operand converted, without change of type domain, type corresponding real typelong double.otherwise, if corresponding real type of either operand
double, other operand converted, without change of type domain, type corresponding real typedouble.otherwise, if corresponding real type of either operand
float, other operand converted, without change of type domain, type corresponding real typefloat.62)otherwise, integer promotions performed on both operands. following rules applied promoted operands:
if both operands have same type, no further 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.
2 values of floating operands , of results of floating expressions may represented in greater range , precision required type; types not changed thereby.63)
62) example, addition of
double _complex,floatentails conversion offloatoperanddouble(and yieldsdouble _complexresult).63) cast , assignment operators still required remove range , precision.
and integer promotions defined in §6.3.1.1:
6.3.1.1 boolean, characters, , integers
1 every integer type has integer conversion rank defined follows:
- no 2 signed integer types shall have same rank, if have same representation.
- the rank of signed integer type shall greater rank of signed integer type less precision.
- the rank of
long long intshall greater rank oflong int, shall greater rank ofint, shall greater rank ofshort int, shall greater rank ofsigned char.- the rank of unsigned integer type shall equal rank of corresponding signed integer type, if any.
- the rank of standard integer type shall greater rank of extended integer type same width.
- the rank of
charshall equal rank ofsigned char,unsigned char.- the rank of
_boolshall less rank of other standard integer types.- the rank of enumerated type shall equal rank of compatible integer type (see 6.7.2.2).
- the rank of extended signed integer type relative extended signed integer type same precision implementation-defined, still subject other rules determining integer conversion rank.
- for integer types
t1,t2, ,t3, ift1has greater rankt2,t2has greater rankt3,t1has greater rankt3.2 following may used in expression wherever
intorunsigned intmay used:
- an object or expression integer type (other
intorunsigned int) integer conversion rank less or equal rank ofint,unsigned int.- a bit-field of type
_bool,int,signed int, orunsigned int.if
intcan represent values of original type (as restricted width, bit-field), value convertedint; otherwise, convertedunsigned int. these called integer promotions.58) other types unchanged integer promotions.3 integer promotions preserve value including sign. discussed earlier, whether ‘‘plain’’
chartreated signed implementation-defined.58) integer promotions applied only: part of usual arithmetic conversions, argument expressions, operands of unary
+,-, ,~operators, , both operands of shift operators, specified respective subclauses.
Comments
Post a Comment