c - What happens if I sum negative values to a 16bit register constantly -
i stuck in project. finding average of multiple values. have ten values average. sum giving correct result time adding negative numbers , result negative numbers, when values average found has high magnitudes 16bit register in summmation stored gives positive value. going wrong in here.
void smootharray() {      val = 0;     for(k=0;k<10;k++)     {         cc = array[k];         val += cc;      }     val /= 10;  } in when array has many locations negative summation wrong positive. c language used. array unsigned short, while val short
what happens if sum negative values 16bit register constantly
you have undefined behavior. let's example int 16-bit , adding 2 int value int_min , -1, invoke undefined behavior. in systems end positive value.
use variable of wider type sum values, example declare val of type long guaranteed @ least 32-bit wide.
Comments
Post a Comment