F
Fred
I'm having terrible trouble trying to work out exactly which of the
promotions one reads about in old books are still present in current C -
and there seems to be a distinction between promotion for function
arguments versus expressions?
For example, consider the following code:
short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */
Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
(4. And as usual we discard the value of the assigment expression)
Is this also why there are suffixes for long and unsigned integer
constants, but no suffixes for shorts and chars? Because these
automatically become ints as soon as they appear?
It does seem unnecessarily indirect to do this promotion and demotion
all the time - half of one's compiled code must end up just moving back
and forth between different integer types!
promotions one reads about in old books are still present in current C -
and there seems to be a distinction between promotion for function
arguments versus expressions?
For example, consider the following code:
short u; /* declare u as a short */
(void) (u=10); /* set u to 10 */
Am I right in thinking the following happens:
1. 10 is promoted to an int.
2. This expression is evaluated as an int to yield 10.
3. Then the result is demoted to a short (equal to 10) and stored in u.
(4. And as usual we discard the value of the assigment expression)
Is this also why there are suffixes for long and unsigned integer
constants, but no suffixes for shorts and chars? Because these
automatically become ints as soon as they appear?
It does seem unnecessarily indirect to do this promotion and demotion
all the time - half of one's compiled code must end up just moving back
and forth between different integer types!