Need some help understanding a weird C behavior
Posted
by mike
on Stack Overflow
See other posts from Stack Overflow
or by mike
Published on 2010-04-10T20:05:23Z
Indexed on
2010/04/10
20:13 UTC
Read the original article
Hit count: 180
Filed under:
c
This part of my code works fine:
#include <stdio.h>
int main(){
//char somestring[3] = "abc";
int i, j;
int count = 5;
for((i=0) && (j=0); count > 0; i++ && j++){
printf("i = %d and j = %d\n", i, j);
count--;
}
return 0;
}
The output as expected:
i : 0 and j : 0
i : 1 and j : 1
i : 2 and j : 2
i : 3 and j : 3
i : 4 and j : 4
Things get weird when I uncomment the char string declaration on the first line of the function body.
#include <stdio.h>
int main(){
char somestring[3] = "abc";
...
}
The output:
i : 0 and j : 4195392
i : 1 and j : 4195393
i : 2 and j : 4195394
i : 3 and j : 4195395
i : 4 and j : 4195396
What's the logic behind this? I'm using gcc 4.4.1 on Ubuntu 9.10.
© Stack Overflow or respective owner