I can't figure out what I did wrong. I want to print non-zero elements and the below code prints none.
#include <stdio.h>
int main ()
{
int arr[4] = { 0, 3, 0, 7 };
// print non zero elements
for (int i = 0; i != 4 && arr[i] != 0; ++i)
printf("%d\t%d\n", i, arr[i]);
}
But if I move the array test like the below, it works:
#include <stdio.h>
int main ()
{
int arr[4] = { 0, 3, 0, 7 };
// print non zero elements
for (int i = 0; i != 4; ++i) {
if (arr[i] != 0)
printf("%d\t%d\n", i, arr[i]);
}
}
What's wrong?