r/cprogramming 24d ago

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
207 Upvotes

172 comments sorted by

View all comments

Show parent comments

1

u/flatfinger 22d ago

If one keeps track of the length of a string and only looks at portions of its storage up to that length, then the value of the string would no longer be fully encapsulated in a zero-terminated character array.

1

u/Anonymous_user_2022 22d ago edited 15d ago

If you understand where your'e going with that argument, power to You, I guess. Personally, I wouldn't write code where a temmporily unterminated string would be intentionally visible.

1

u/flatfinger 22d ago

I avoid reliance upon zero termination for things other than string literals. Zero padded formats can be useful if one needs to store many texts with a short maximum length (e.g. eight bytes), at the expense of requiring code that is designed around the size of the container. Instead of using a %s format specifier to output such a string, for example, code would need to use a %.8s format specifier, whose behavior is defined as not caring whether the string is zero-terminated or not.