https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96538
Andrew Pinski <pinskia at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |RESOLVED Resolution|--- |INVALID --- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> --- There is NO BUG here: sum = arr[1]+arr[2]+arr[3]; Is done in integer type and then casted to long. that is it is the same as: sum = (long)(arr[1]+arr[2]+arr[3]); While: for(int j=0;j<5;j++) { sum = sum+ arr[j]; } Is Done in long type. You can fix the original code by doing: sum = ((long)arr[1])+((long)arr[2])+((long)arr[3]);