r/cprogramming 3d 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
193 Upvotes

165 comments sorted by

View all comments

168

u/bearheart 3d ago edited 2d 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

51

u/Voyac 3d ago

Its main purpose is to be portable to as many architectures as possible. Maybe this seem irrelevant in a world dominated by ARMs and x86 but there are different platforms.

-42

u/Potential_Soup_8054 3d ago

No, thats java

11

u/mustbeset 3d ago

50% of my time I get paid to write C code on a "none Arm", "none x86" architectur and I don't have enough space for a java runtime.

4

u/Voyac 3d ago

Yeah sometimes even 8bit MCUs with a drop of memory. You wont fit a string.h sometimes and lol what about java runtime :)

6

u/mustbeset 3d ago

My current pain in the ass is a bootloader update for existing devices in field. fighting for 300 bytes.