I don\'t know if I\'m just being a total fool, most likely I am, it\'s been a long day, but this isn\'t working as I want it to, and, well, I don\'t see why.
It sho
My guess is buffer over-run since the for-loop reads in 11 numbers and the 11th number gets stored outside the array, probably overwriting i.
Try changing the 11 to a 10 in the for loop.
You're storing eleven numbers into an array of size 10. Thus you're storing the last element out of bounds, which invokes undefined behavior.
The reason that this undefined behavior manifests itself as an infinite loop in your case is probably that i
is stored after array
in memory on your system and when you write a number into array[10]
(which is out of bounds, as I said), you're overwriting i
. So if you entered a number smaller than 11 this will cause the loop to continue and ask for input once more.
Try this:
void main() {
int array[10];
int i;
int sum = 0;
for ( i = 0; i < 11; i++){
scanf("%d", &array[i]);
}
for (i = 0; i < 11; i++) {
sum = sum + array[i] ;
}
printf("%d", sum);
return 0;
}