r/cprogramming • • May 05 '26

How to do strings properly?

I just started learning C and I often read on the internet that it is bad practice to use null terminated strings, so my question is what should I do instead?

31 Upvotes

83 comments sorted by

View all comments

Show parent comment

1

u/Lopsided-Cost-426 May 08 '26

array of characters (typically 1-byte width)

The C standard mandates sizeof(char) be equal to 1 so a char will always be 1 byte long. That being said there are some extremely old systems that have bytes not equal to 8 bits so while a char is always 1 byte long, it isnt necessarily 8 bits long.

1

u/flatfinger May 11 '26

There are even rarer systems which allow memory to be accessed in chunks smaller than 8 bits. If e.g. a system used 4-bit addressable units, a C compiler for such a system would be required to store pointers in such a manner that adding 1 to a char* would increase the stored value by 2, have attempts to dereference pointers scale up the stored value by 2 prior to use, treat pointers as indices into two parallel arrays of four-bit values, or do something else unusual to accommodate the fact that a char* couldn't be the system's smallest addressable unit of storage.