Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You might not want that unconditional promotion in a systems programming language.

The problem in C that you can avoid is not taking into account the destination type of a calculation.

If you have in16 + int16, being assigned, returned or passed to a int32 type, then the calculation can be done in int32.

If the result is going back to an int16 type, then there is no need to go to int32.

In C expressions, the types are almost purely synthesized attributes: what that term means is that the information flows up the abstract syntax tree from the children to the parents. in a = b + c, the type of (b + c) is determined without regard for the parent = node. This is very simple and has the benefit of being not only easy to understand but in some ways referentially transparent: when we move (b + c) elsewhere, it retains its meaning (except for the part of what happens to the resulting value in the new context). More sophisticated rules can reduce errors though.



> If you have in16 + int16, being assigned, returned or passed to a int32 type, then the calculation can be done in int32.

By the way, if int has 16 bits (which is rare nowadays), then the calculation will happen in 16 bits. If int has more than 16 bits, then both operands will be promoted to that size before the operation.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: