looking for some solutions? You are welcome.

SOLVED: Why does exchanging variable name changes output of C program?


I tried this example of array and post increment/ pre increment on it's elements


int main()
    int j[5] = {5, 1, 15, 20, 25};
    int k , l, m, n;

    n = ++j[1];
    k = ++j[1];
    l = j[1]++;
    m = j[k++];

    printf("\n%d, %d, %d, %d", n, k, l, m );

    return 0;

here the output is :

2, 4, 3, 20

and if i change the order of n and k ie instead of

n = ++j[1];
k = ++j[1];

i write

k = ++j[1];
n = ++j[1];

The output becomes :

3, 3, 3, 15

I tried this on mingw compiler on windows10 and also on Kali Linux's GCC... Same problem.

It is just like taking different variable name alters the output of program. What might be the cause?

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots

No comments: