Your code has two problems.
You didn't initialized the array foo
and foo
has storage-class auto
. The elements contain arbitrary garbage values. Using them in the loop invokes undefined behavior.
'0'
is not equal to 0
. '\0'
is equal to 0
. But better use 0
as you only want 0
in its integer nature.
Surrounding integers in apostrophes make them character constants which have specific value. Most likely these values belong to the ASCII character set. '0'
for example has the ASCII value 48
.
So
if (foo[i] != '0')
is high-probably equivalent to
if (foo[i] != 48)
on your system.
Side Note:
int foo[512];
for (i= 0; i < 512; i++)
You use two times the hardcoded value 512
. This technique is susceptible for failures. Better use f.e. a macro constant instead, which ensures that the values are equal:
#define SIZE_ARR 512
...
int foo[SIZE_ARR];
for (i= 0; i < SIZE_ARR; i++)