Search code examples
cunicodecountstrlen

how does strlen count unicode in c


I'm curious as to how strlen count unicode characters of multiple bytes in C.

Does it count each byte or character (as they can consist of several bytes) until first '\0'?


Solution

  • strlen() counts number of bytes until a \0 is encountered. This holds true for all strings.

    For Unicode, note that the return value of strlen() may be affected by the possible existing \0 byte in a valid character other than the null terminator. If UTF-8 is used, it's fine because no valid character other than ASCII 0 can have a \0 byte, but it may not be true for other encodings.