krzyhub
krzyhub

Reputation: 6540

Understanding C variables

It takes n - range of series - and then ask for elements of that series. Then it should returns max number but it always returns 49. If I use gdb I can say that even before place where this variable is inicialized it has value 49. I don't know why and how to fix this. I have an easy C program like so:

#include <stdio.h>

int T[1000];

int main(int argc, char *argv[])
{
    int n;
    int i;
    int m;
    scanf("%d", &n);

    for (i = 0; i < n; ++i)
    {
        scanf("%d", T + i);
    }

    for (i = 0; i < n; ++i)
        if (T[i] > m)
            m = T[i];

    printf("max: %d\n", m);
    return 0;
}

Upvotes: 0

Views: 44

Answers (1)

ouah
ouah

Reputation: 145899

m is not initialized in your program.

Initialize it to 0:

int m = 0;

Upvotes: 2

Related Questions