I need to print values stored in an int array, stopping when a NULL character is encountered ('\0').
So I have this code:
const int display[10] = {1,4,8,2,0,9,2};
int main(){
int i = 0;
for (i = 0; i < 10; i++){
if (display[i] == '\0'){
break;
}
printf("%d\n", display[i]);
}
exit(0);
}
I expected to print all the values of display[10] OR break when a '\0' was encountered but my code breaks on display[4] (0) instead of continuing until display[6].
Any advice on how to achieve this, avoiding printing the null characters at the end of my array?
\0is just 0, so it breaks at the 0 in your array.const int display[] = {1,4,8,2,0,9,2};and work out the size.0and'\0'are two different ways to write the same thing... same as42and0x2A... orfor (;;) { /*...*/ }andwhile (1) { /*...*/ }