 # Help With Subtotals and Totals in C Program

Hi, I’m very new to C. I have a small program that shows subtotal and total of user input numbers. The total is working but the subtotals after each input return the number 0. Can somebody point out what am I doing wrong here?

``````#include <stdio.h>
#include <math.h>

int main()
{
int j, numbers, total=0, result=0;

printf("This program adds up 5 numbers from the user.\n");
printf("Subtotal will be shown until the final result is reached.\n");

for(j = 0; j <4; j++) {
result += numbers[j];
}

printf("Provide number 1/5\n");
scanf("%d", &numbers);
printf("Your first number is %d", result);

printf("\nProvide number 2/5\n");
scanf("%d", &numbers);
printf("Subtotal is %d", result);

printf("\nProvide number 3/5\n");
scanf("%d", &numbers);
printf("Subtotal is %d", result);

printf("\nProvide number 4/5\n");
scanf("%d", &numbers);
printf("Subtotal is %d", result);

printf("\nProvide the final number\n");
scanf("%d", &numbers);

for(j = 0; j <5; j++) {
total += numbers[j];
}
printf("The final sum is %d", total);

return 0;
}
``````

You don’t update `result` between the `scanf` steps, the numbers typed by the user are only ever used in the final `for` loop that updates `total`.

However, that `result` ends up being zero is luck (or most likely courtesy of your compiler). Your compiler should be warning you about use of uninitialized variables here (if not, switch on warnings!):

At that point you have declared `numbers`, but not initialized it (that is, you didn’t write any data to it). In C uninitialized variables cause undefined behavior, so basically anything could happen there. Apparently your compiler is nice and just returns zero in that case instead of crashing your program. Assuming uninitialized variables will contain zeroes is a common mistake.