'loop with pointer in C

I was testing around with loops in c, and I came across a post about pointers in loops (for(foo = bar; *foo; foo++)).

I tried testing around with this knowledge, and made this:

int main() {
   char a[9] = "test test";
   char *b; 
   for(b = a; *b; b++)
       printf("%c", *b);
   return 0; 
}

I compiled this (gcc test.c), then ran it and got the following output: test test‼ aest test¶ ast test§ at test▬ a test↨ atest↑ aest↓ ast→ ata∟ a aa

I changed char a[9] to char a[10], assuming it had something to do with \0, then got test testest testst testt test testtesteststt.

Why does this happen? I thought that this loop would end after 9 cycles, as there are 9 characters in the string provided.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source