The array str
takes 10 bytes from stack of the application.
Some compilers calculate the required stack size by simply sum the number of bytes of all variables, arrays and structures located on stack in all functions plus the number of bytes all function arguments and the function calls would need. But this stack size calculation is definitely always wrong as not all functions are called in a sequence.
Therefore many compilers often define a fixed stack size which is allocated on start of the application before main is called. The stack size can be controlled by a developer, for example when an application needs more than applications usually need because of lots of recursive function calls.
For example, take a look on the Visual Studio page about /F (Set Stack Size). The default stack size for C/C++ applications compiled with Visual C/C++ have 1 MB of stack.
Therefore it is no problem on your simple code that a string with more than 10 bytes is copied on stack to the location on which str
points. The string from scanf
is terminated with a null byte. printf
simply outputs bytes until a null byte is found.
But if you would modify your code by adding a function which contains for example also an array located on stack which is additionally also initialized with something, and this function is called between scanf
and printf
, you would see a different string on output as used on input.
scanf
and printf
should be both not used anymore because of no check on array sizes. The C/C++ libraries and the various frameworks/libraries offer more secure functions which check the sizes of the string arrays.