What is a null-terminated String?

Technology CommunityCategory: StringsWhat is a null-terminated String?
VietMX Staff asked 3 years ago

A “string” is really just an array of chars; a null-terminated string is one where a null character '\0' marks the end of the string (not necessarily the end of the array). All strings in code (delimited by double quotes "") are automatically null-terminated by the compiler.

So for example, "hi" is the same as {'h', 'i', '\0'}.

Null-terminated strings are often a drain on performance, for the obvious reason that the time taken to discover the length depends on the length. The usual solution is to do both – keep the length and maintain the null terminator. It’s not much extra work and means that you are always ready to pass the string to any function.