On Sat, Sep 21, 2013 at 07:27:20PM +0100, Jonathan Wakely wrote: > On 21 September 2013 19:09, <wempwer@xxxxxxxxx> wrote: > > > > It tried -O2 and it also worked. However on my system it only said: > > > > warning: integer overflow in expression > > > > How did you get gcc to produce this part: > > > > [-Woverflow] > > > > I use gcc 4.5.2. But when I use gcc like this I also do not get any > > warnings: > > > > gcc -Woverflow main.c -o main > > 4.5.2 is quite old now. More recent versions show the warning option > that triggered the warning, that's what the [-Woverflow] part is. > > > I wonder: > > > > 1) why -Woverflow appears in your gcc output? > > Because it's a recent version. > > > 2) why `gcc -Woverflow main.c -o main' does not produce a warning? > > Because without optimisation the compiler doesn't do the necessary > analysis to realise that in the multiplication expression it knows the > values. > > > 3) why does `gcc -O main.c -o main' produce warning in the first place > > and why does it do this only when two operands are const? > > Because the optimiser can track the values of constants (because they > don't change) and see that the values in the multiplication are known. But compiler also knows the operand values even if they are not constant? These values must be known to produce the multiplication result. Is this warning produced only when two operands are constant and optimization is turned on because this is just the way gcc does it and the warnings could also be produced for non-const operands if a compiler worked in a different way? -- <wempwer@xxxxxxxxx>