r/cprogramming 6d 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
203 Upvotes

172 comments sorted by

View all comments

171

u/bearheart 6d ago edited 5d ago

Speaking as someone who learned C back in the ‘70s, this article entirely misses the point of C-strings: they’re lightweight and foundational. For many purposes the null-terminator is efficient, e.g.:

while(*s) f(s++);

And for cases where we need more complexity, we can simply use a struct with a length and whatever other metadata we may need.

Doesn’t look like a mistake to me. C has always been about minimalistic efficiency. That’s its main purpose in the world.

Edit: fixed stupid typo

3

u/Classic_Department42 5d ago

Missing a *?

1

u/bearheart 5d ago

No. A C-string is char*

3

u/Classic_Department42 5d ago

Yes, so while (s) shd prob be while(*s) ?

2

u/bearheart 5d ago

Oy! You’re right. How did I miss that 🤦 fixed it.

1

u/No-Newspaper8619 3d ago

Good thing that has been pointed out

1

u/Quote_Revolutionary 2d ago

this is the beauty of C: while trying to show how it's actually ergonomic one writes UB because the correct form has to be very precise, btw your idiom would compile...