0
#include <stdio.h>

int main(){
    int n,sum;
    printf("Enter 'n' to get the sum of numbers from 1 to 'n': ");
    scanf("%d", &n);
    
    while (n>0){
        sum+=n;
        n-=1;
    }
    
    printf("The sum is %d", sum);
}

I wrote a code getting input of 'n' and returning the sum of the numbers from 1 to 'n'. When putting 10, I supposed it would return me 55.

Enter 'n' to get the sum of 1 to 'n': 10
The sum is 32820

But in fact, it returns 32822 or something like this, which I don't get it. It seems to be related to 32767+55=32822.

I also looked for what happens if a variable is declared but not initialized. One says if a declared variable is not initialized, it refers to the outdated data, which is used by another code that used that same memory address.

Why is the variable recalling an awkward value?

0 Answers0