That's where a C convention for strings steps in. By convention, when the compiler sees a string "between quotes"
, it stores the ASCII values of the characters, and then follows them with a 00
. Not an ASCII '0'
(with value 48), but an ASCII NUL
(with value 0). Thus if you looked at address 0x3004
(the byte directly after John
), you'd see a 0 in that memory byte. That's how printf()
knows to stop printing - and yes, many a bug has resulted from forgetting to maintain a NUL
at the end of a sequence of chars!