strlen
returns the number of characters that precede the terminating null character. An implementation of strlen
might look like this:
The behavior of the variant that you are showing is well defined under these circumstances.
0
.char
value can be accessed without UB, the clauses about trap representations in the standard explicitly exclude all character types from that.char
value.for
loop stops at latest at position 9
, so you will not overrun your array.So no "bad" things beyond the visible may happen if you use your specific version of the function. But having a function call that produces unspecified results is certainly nothing you want to see in real code. Something like this here leads to very subtle bugs, and you should avoid it by all means.