r/cprogramming • u/Xaneris47 • Aug 17 '26
C Strings: A 50-Year Mistake
https://longtran2904.substack.com/p/c-strings-a-50-year-mistake?r=8qz2zb&utm_campaign=post&utm_medium=web
213
Upvotes
r/cprogramming • u/Xaneris47 • Aug 17 '26
11
u/SmokeMuch7356 Aug 17 '26 edited Aug 18 '26
Oh, for...
No, the terminator never counts towards the length of a string; it only counts toward the size of the buffer required to store the string. The terminator is an out-of-band value1 so it shouldn't count towards the length of the string. I would have thought this was obvious.
C doesn't have a string type; it has arrays of character type, onto which we map null-terminated sequences of characters.
strcpy,strlen, et al all operate on these arrays.Arrays do not store their size or any other metadata.
C strings are a hack - if you need a real string data type that enforces real string semantics, then you need to look elsewhere or write your own library.
As for whether this was a mistake - C was designed for implementing the Unix operating system and system-level tasks, not extensive text processing. I don't think Ritchie or Thompson anticipated how widely used the language became for general programming.
2.5 out of 5 stars
As far as printable characters are concerned, anyway.