Behavior of char

Can someone please help, I don't understand why the code below has output likes

1234
abcd1234

but NOT

1234
abcd

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int main() {
  char test1[] = "1234";
  char test2[4] = "abcd";

  printf("%s\n", test1);
  printf("%s\n", test2);

  getchar();
}
Because test2 is not null-teminated char array. You got your output because you got lucky and nothing has crashed.

test1 array contains five characters: {'1', '2', '3', '4', '\0'} and is a proper nul-terminated string (\0 as last character) required by %s format specifier.
oh, I got it....thanks MiiNiPaa

if I changed to test2[5] = "abcd" , then it works as expected. Thank you!!!
Topic archived. No new replies allowed.