On 10/13/2011 04:08 PM, steve wrote:
> When compiling the program
>
> int main(void)
> {
> short a;
>
> a = a + a;
>
> return 0;
> }
>
> with gcc 4.3.3 -Wconversion I get the warning:
>
> warning: conversion to 'short int' from 'int' may alter its value.
>
> It looks like 'a' got promoted to an int. But I thought operands only
> got promoted to the lowest type that included all the operands.
For "An object or expression with an integer type whose integer
conversion rank is less than or equal to the rank of int and unsigned
int" or "A bit-field of type _Bool, int, signed int, or unsigned int",
"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.4

All other types are
unchanged by the integer promotions." (6.3.1.1p2).
> FWIW,
> I got the same warning when I changed the line to a=a+1. But I did
> NOT get the warning when I just had a++.
>
> Or is this warning completely bogus?
Your code also involves a conversion of 'int' back to 'short int', and
that's what the waring is about. That conversion can cause problems if,
before the sum, a > SHRT_MAX/2 || a < SHRT_MIN/2.