c - How possibly can a unary +/- operator cause integral promotion in "-a" or "+a", 'a' being an arithmetic data type constant/variable? -


this seemingly trivial line taken c book mike banahan & brady (section 2.8.8.2).

i can understand how implicit promotion comes play in expressions c=a+b depending on types of operands, unable grasp how , in case same can figure in -b, b legitimate operand. can explain , give proper example?

extracted text follows:

the usual arithmetic conversions applied both of operands of binary forms of operators. integral promotions performed on operands of unary forms of operators.

update:

lest goes unnoticed, here adding asked based on ouah's answer in comment– book says 'only integral promotions performed'...does mean in expression x=-y, 'x' long double , 'y' float, 'y' won't promoted long double if explicitly use unary operator? know be, asking nevertheless clearer idea "only integeral promotions..." part.

update:

can explain example how promotion comes play following bit-wise operators? last three, should assume whenever used on variable, promoted integer type first? , "usual arithmetic conversions" mean first three? can give small example? don't want post separate question if can settled here.

enter image description here

for unary arithmetic operator, c standard says (in section 6.5.3.3) that

the integer promotions performed on operand, , result has promoted type.

it defines term, in section 6.3.1.1:

if int can represent values of original type (as restricted width, bit-field), value converted int; otherwise, converted unsigned int. these called integer promotions. other types unchanged integer promotions.

(references n1570 draft of 2011 c standard.)

i believe rationale implementations not required support arithmetic operations on types narrower int (one "word"). operands narrower int converted int, or unsigned int, before they're operated on.

for binary operators (those taking 2 operands), there's additional requirement, both operands must of same type. typical cpus might have instructions add 2 32-bit signed integers, or 2 32-bit unsigned integers, or 2 64-bit signed or unsigned integers, none directly add, example, 32-bit signed integer , 64-bit unsigned integer. allow this, have usual arithmetic conversions, described in section 6.3.1.8. these rules tell you, example, happens when try add int double: int operand promoted, conversion, type double, , addition adds resulting 2 double operands.

the shift operators don't require usual arithmetic conversions, since there's no particular need both operands of same type. left operand value operated on; right operand specifies number of bits shift it.

does mean in expression x=-y, x long double , y float, y won't promoted long double if explicitly use unary operator?

assignment causes right operand converted type of left operand. expression -y evaluated independently of context in appears (this true expressions). unary - applied operand, of type float (the integer promotions don't affect that), yielding result of type float. assignment causes float value converted long double before being assigned x.

the title of question asks how can possibly happen. i'm not sure means. conversion rules specified in language standard. compilers follow rules.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -