Hello
The result of an expression using pre-decrement or pre-increment such as:
y = x * n * --n;
gives different results in a few cases where x is placed before or after
the rest, or when its value is 1 or not. Please run the program attached
where a comment indicates what we think works wrong. We obtain:
1.- 16
2.- 16
3.- 16
4.- 40
5.- 20
6.- 16
7.- 40
8.- 32
1.- 36
2.- 36
3.- 36
4.- 60
5.- 30
6.- 36
7.- 60
8.- 72
In contrast, the results we get running it in a AIX Computer with the
native compiler (IBM XL C/C++ Enterprise Edition for AIX, V9.0 Version:
09.00.0000.0000) is what we expected:
1.- 20
2.- 20
3.- 20
4.- 40
5.- 20
6.- 20
7.- 40
8.- 40
1.- 30
2.- 30
3.- 30
4.- 60
5.- 30
6.- 30
7.- 60
8.- 60
We have experienced this issue in several versions of the gcc compiler
(the ones distributed under fedora cores 6, 7, 8, knoppix 5.0.1 and also
in the windows Dev-c++ 4.9.9.2 package compiler) obtaining the same
results, that we think are (obviously) wrong.
We remain expectant of any comment from you.
Best regards
Miguel Ángel Quintáns & Virginia Escuder
Professors at the Universidad de Alcalá
Madrid
Spain
#include <stdio.h>
int main () {
int x=2;
int p=1;
int n=5;
int y = n * --n;
printf ("1.- %d\n",y); // wrong
n=5;
y = 1 * n * --n;
printf ("2.- %d\n",y); // wrong
n=5;
y = n * --n * 1;
printf ("3.- %d\n",y); // wrong
n=5;
y = 2 * n * --n;
printf ("4.- %d\n",y);
n=5;
y = p * n * --n;
printf ("5.- %d\n",y);
n=5;
y = n * --n * p;
printf ("6.- %d\n",y); // wrong
n=5;
y = x * n * --n;
printf ("7.- %d\n",y);
n=5;
y = n * --n * x;
printf ("8.- %d\n",y); // wrong
n=5;
y = n * ++n;
printf ("1.- %d\n",y); // wrong
n=5;
y = 1 * n * ++n;
printf ("2.- %d\n",y); // wrong
n=5;
y = n * ++n * 1;
printf ("3.- %d\n",y); // wrong
n=5;
y = 2 * n * ++n;
printf ("4.- %d\n",y);
n=5;
y = p * n * ++n;
printf ("5.- %d\n",y);
n=5;
y = n * ++n * p;
printf ("6.- %d\n",y); // wrong
n=5;
y = x * n * ++n;
printf ("7.- %d\n",y);
n=5;
y = n * ++n * x;
printf ("8.- %d\n",y); // wrong
}