The format specifier %s
is used to output strings that is sequences of characters terminated with zero characters.
You declared the array the single (first) element of which does not contain a string.
char ss[][3] = { "rty" };
In fact the array is declared the following equivalent way
char ss[][3] = { { 'r', 't', 'y' } };
that is the terminating zero of the string literal was excluded from the list of initializers because the size of the internal array is equal to only 3.
To output the array you could write
printf("%3.3s\n", ss[0]);
explicitly specifying the number of characters you are going to output.
If you want to output it as a string you should enlarge it like
char ss[][4] = { "rty" };
that to include the terminating zero of the string literal "rty"
.
In case of the original program it seems that the compiler placed in the stack the arrays in the following order ss
and then s
. That is the memory allocated to the arrays looks the following way.
{ 'r', 't', 'y', 'a', 'z', 'e', '\0' }
|___________| |_________________|
ss s
Pay attention to that this declaration
char s[] = "aze";
is equivalent to
char s[] = { 'a', 'z', 'e', '\0' };
that is the string literal includes the terminating zero and consequently the array s
will contain a string.
Also you should know that such a declaration
char ss[][3] = { "rty" };
is not allowed in C++. In C++ you have to write at least like
char ss[][4] = { "rty" };