• Create Account

#ActualCornstalks

Posted 09 October 2012 - 02:50 PM

To clarify this a little bit, the C standard says this about evaluating an integer expression:

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

If I'm understanding that right, on a 32-bit system (that is, when int is 32-bits), then yes, you'd get the right result of -104 because each operand is implicitly promoted to an int. But on a system where int is smaller than 32-bits, (like if int is 16-bits), then you'll have overflow. Which is exactly why you need to either use bigger datatypes or use that cast.

#1Cornstalks

Posted 09 October 2012 - 02:49 PM

To clarify this a little bit, the C standard says this about evaluating an integer expression:

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

If I'm understanding that right, on a 32-bit system (that is, when int is 32-bits), then yes, you'd get the right result of -104. But on a system where int is smaller than 32-bits, (like if int is 16-bits), then you'll have overflow. Which is exactly why you need to either use bigger datatypes or use that cast.

PARTNERS