C natively does not have any boundary checking. Access out of bound memory is undefined behavior. It can cause run-time error (in form of segmentation fault) or simply overwrite some other memory area (corrupting some other variable/memory location) and run fine. IT's simply undefined.
That said, the purpose of having (using) snprintf()
is to supply the length properly to avoid out of bound memory access over sprintf()
. You should use it in proper way, something like
snprintf(str,sizeof(str),"0123456789012345678");
because, as mentioned in C11
, chapter §7.21.6.5
[...] Otherwise, output characters beyond the n-1
st are
discarded rather than being written to the array, and a null character is written at the end
of the characters actually written into the array. [..]
which makes sense of using snprintf()
over sprintf()
.