Search code examples
arraysccharprintfc-strings

C printf prints an array that I didn't ask for


I have recently started learning C and I got into this problem where printf() prints an array I didn't ask for. I was expecting an error since I used %s format in char array without the '\0', but below is what I got.

char testArray1[] = { 'a','b','c'};
char testArray2[] = { 'q','w','e','r','\0' };

printf("%c", testArray1[0]);
printf("%c", testArray1[1]);
printf("%c\n", testArray1[2]);

printf("%s\n", testArray1);

the result is

abc
abcqwer

thanks


Solution

  • %s indeed stop when encountered \0, but testArray1 didn't have that \0, so it keeps printing the following bytes in the memory.

    And the compiler magically(actually intentionally) places the testArray2 next to testArray1, the memory is like:

    a b c q w e r \0
    ^                testArray1 starts here
          ^          testArray2 starts here
    

    And the %s will print all of those chars above until it meets a \0.

    You can validate that by:

    printf("%d\n", testArray2 == testArray1 + 3);
    // prints `1`(true)
    

    As your question, there was no error because the a ... r \0 sequece in memory is owned by your process. Only the program is trying to access an address not owned by it, the OS will throw an error.