The way you define your two "strings", gamesA
and gamesB
, it is very likely that they are linked consecutively in memory. I.e. that gamesB
starts at a low address and ends right before gamesA
, i.e. the last byte belonging to gamesB
is immediatly followed by gamesA
.
gamesB gamesA
[0][1][2][3][4][0][1][2][3][4]
Note that above is speculation. There are no guarantees for this. But often compilers/linkers do it that way.
Now lets see what happens in the situation you describe.
When gamesB is full (contains 5 characters) ...
Well that is the core of the matter, when there are 5 characters read by a scanf("%s", X)
into that array of characters of size 5, then that array is actually too small, because there will be a 6th character written, the terminating '\0'
.
That terminator will be written one byte beyond the last byte owned by gamesB
and end up in the first byte owned by gamesA
. This situation means that there is an error in the program (or if you want to see it like that, in the input by the user; but a program should live with any input...).
gamesB gamesA
[0][1][2][3][4][0][1][2][3][4]
"a b c d e \0""B C D \0 "
In this erroneous situation, you find some 0-terminated characters in gamesB
, wrong, but easily printable by anything expecting a 0-terminated character sequence.
In this situation you find in gamesA
NO characters whatsoever, and that sequence of no characters is nicely 0-termianted right in the first byte of gamesA
. Whatever you had in gamesA
is ignored by anything expecting a 0-terminated character sequence.
Just for fun, try inputting 6 characters to gamesB
and then print both. You get the whole sequence output as gamesB
and just the last character gamesA
.
gamesB gamesA
[0][1][2][3][4][0][1][2][3][4]
"a b c d e f \0""C D\0"