Because you did not NULL-Terminate your string.
function printf
will print a string until it finds a \0
byte that indicates the end of the string.
You did not put in a \0
byte, so printf
continues to print until it encounters a terminator just by sheer luck.
In this case, it looks like it printed 3 more characters of random bytes until it happened to find the end of string marker.
To fix it, do this:
char name[6] = {'A', 'B', 'C', 'D', 'E', '\0'};
// Changed the size to 6, and added the NULL-terminator (aka. End-of-String Marker)