What’s the rationale for null terminated strings?

Technology CommunityCategory: StringsWhat’s the rationale for null terminated strings?
VietMX Staff asked 3 years ago
Problem

Why would null terminated strings have been chosen instead of the obviously superior length prefixing?

At the time C (and the languages that it was derived from) were developed, memory was extremely limited, so using only one byte of overhead to store the length of a string was attractive. The only popular alternative at that time, usually called a “Pascal string” (though also used by early versions of BASIC), used a leading byte to store the length of the string. This allows the string to contain NUL and made finding the length need only one memory access (O(1) (constant) time). But one byte limits the length to 255. This length limitation was far more restrictive than the problems with the C string, so the C string in general won out.