My C has a behavior I do not understand.
I have defined an array as following
long gainT1[21];
I do some work with that variable (so, no need for an initialization), then, later, I would like to display the values that it contains. And here comes the problem: The first value showed by the second for is not the expected one!
printf("CHECKING: gainT1[0]=%ld\n", gainT1[0]);
{
ptrdiff_t k;
for(k = 0; k < 16; k++) printf("[%td]=%ld ", k, gainT1[k]); printf("\n");
for(k = 0; k < 17; k++) printf("[%td]=%ld ", k, gainT1[k]); printf("\n"); // Here the for is going up to 16 instead of 15 previously
}
this code returns:
CHECKING: gainT1[0]=4207440
[0]=4207440 [1]=4207440 [2]=4294967295 [3]=139846275105333 [4]=16351504 [5]=0 [6]=139846268659528 [7]=139846277253568 [8]=16351504 [9]=3 [10]=128 [11]=139846277252304 [12]=4294967295 [13]=139846272645590 [14]=0 [15]=0
[0]=2356216002 [1]=4207440 [2]=4294967295 [3]=139846275105333 [4]=16351504 [5]=0 [6]=139846268659528 [7]=139846277253568 [8]=16351504 [9]=3 [10]=128 [11]=139846277252304 [12]=4294967295 [13]=139846272645590 [14]=0 [15]=0 [16]=6312008
I can't figure out what is wrong with my code.