r/C_Programming • u/alex_sakuta • May 27 '26
Discussion Will we ever have length based strings?
Edit: The answer that I find the most correct. No, because null terminated byte strings are allowing the user the flexibility of having their own version of length based strings.
Not having metadata is actually a good thing because metadata would require preallocated space and as everyone knows C gives power to the users to make such decisions.
C is correcting a lot of its mistakes or adding tools to aid the developers in doing so such as attributes, nullptr, fixed-width integers, defer, etc.
So why have I not heard of any draft for length based strings instead of null terminated strings?
Why not create an entirely new library for those?
It's not as hard compared to other changes they are making in my opinion.
For anyone, if you are gonna tell me that null terminated strings work fine or because we can create our own version of this string, here is the reply for that.
I know we can and I do that a lot and I know people just modularize it so they never need to reimplement it again and again. But having something in the standard is far better than having everyone know what and how to implement because there'll always be someone who doesn't.
64
u/fixermark May 27 '26
If you're talking about the standard: I do not know the current state of the discourse on that.
If you're talking about what programmers can do today: most C compilers support
\p"hello", which is the "Pascal string." The first byte is length of the string, and the rest are contents. This limits your string to 255 bytes (plus length specifier). It was extremely common in old MacOS because the original Mac Toolbox (their name for the OS standard library / kernel features) used Pascal strings as its standard for all its APIs.Broadly speaking, the C standards folks are very conservative about new features, because every feature impacts every other existing feature in an O(n2 ) complexity fashion at least. C++ is not, which is why the standard is longer than the King James Bible and some 20% of it is "If you want to use these two features at once: don't. Undefined behavior or IFNDR."
52
u/nnotg May 27 '26
Why not just write your own? Like every other non-standard data structure?
20
u/EpochVanquisher May 27 '26
Realistically, you would end up with two types of strings: nul-terminated and length-based. And that would suck—two sets of APIs for strings.
It’s not hard, but it would create a mess.
You say that you know we can already have that by writing our own library. But there’s your answer. Balance all of the advantages (not many) versus the disadvantages (messy), and you end up with the answer “no”.
To be honest I think the people who complain about missing features in C should be more open-minded about using other languages.
6
u/SmokeMuch7356 May 27 '26
Probably not, for several reasons:
This is an idea someone has every year, but once they start thinking about it in depth they decide it's more work than it's worth. If you really, truly need a "real"
stringtype, C++ is right down the hall.4
u/jason-reddit-public May 27 '26
NUL terminated strings aren't so bad if they are read only and constructed sensibly (either string literals or in my library, from a buffer abstraction (like string builder in Java)).
31
u/DreamingElectrons May 27 '26
I wouldn't call those things mistakes those were the language design constraint at the time. C always allowed you to create a struct that stores a string and a length, it is such a trivial thing to do that adding a new data type for that would simply be cluttering up the language. For the most parts, C only contains what actually is needed to get the work done or to build your own tools for it. If you want a language that comes with an entire toolshed full of (sometimes unnecessary and confusing) options, use C++, you can still use it like it's basically just C.
30
u/flyingron May 27 '26
Not much call for it. Those who really care about such things tend toward C++.
5
u/Maleficent_Bee196 May 27 '26
you can just do it yourself! Your own 'standard' library!!
4
u/TUSF May 27 '26
Features like attributes, nullptr and defer are solving issues that the C standard did not have an easy work around for devs to use without resorting to non-standard extensions.
Length based strings, on the other hand? The standard library already has duplicates for many functions that operate on strings, but which take a length parameter. And every C library that wants to work on length-based strings already provides a string struct, which can only really be written one of two ways, and the one I've seen most often is:
And other languages with a string or slice type generally do the same, but with some syntax sugar to obfuscate that. So I don't see a dedicated string type being added to the C standard
4
u/ntsh_robot May 27 '26
this is easy to implement
try it yourself
4
u/Remus-C May 27 '26 edited May 27 '26
Some already have this, after a bit of work. However, for a standard... I dunno if I want this because of ... Who else want this?
I don't feel that's the C philosophy, to create unmanageable complexity for something supposedly to become a widely used standard. Because if it will not be widely adopted then there is no standard to think of.
Yes, the intention is good. Like many other. (What about a standard GUI library then? Probably many wish that, but that story don't fit in a comment.) However, there is way more that is visible on the surface, for a standard.
3
u/Hungry-Internet1868 May 27 '26
In my opinion, if you are referring to something like a dynamic string container like std::string in C++, there will not be a standard string container in the C standard library in the near future. Even if there will be a standard string container in the future, programmers who have been using their own APIs may still continue to use their own versions because the standard version will not be available if they have to rely on older compilers.
It is a known fact that C lacks standard data containers which are common in the libraries of other programming languages. However, the limitation does not prevent us from programming in C because we have the following options.
That is just the reality of software development in C.
3
u/viva1831 May 27 '26
Imo, creating a strong enough library that gains widespread usage would be the first step (sds with some changes for example)
The second is convincing one or another compiler to included it as a non-standard feature
Then the final step would be to make a proposal to the next c standards working group
3
u/ByronScottJones May 28 '26
I think you are vastly underestimating what's involved. It's not just creating a string library. It's the billions of lines of existing code that expect strings to be null terminated char arrays. If you create a new string type, none of that existing code will be able to use it. So you'll inevitably have to create unboxing and reboxing functions to translate to regular strings and back. You'll just be creating new errors where those boxing functions are used.
It's important to remember that C was created as a portable systems language, and the initial core version of the language was little more than syntactic sugar over assembly. The original version only had 27 keywords in the entire language. Every library is built on top of that, and below that it's simple enough to make the initial bootstrapping compiler for new architectures simple to create.
8
u/Interesting_Debate57 May 27 '26
I think you have C confused with a language where you have no idea what's happening behind the scenes.
Most of those things create abstractions.
Note that C is not an object-oriented language.
You might want to go use C++. It's not significantly slower, but because it is object oriented, it's possible to shoot yourself in the foot with obfuscation due to clever abstraction.
Trying to "fix" C is pretty pointless.
2
u/Lord_Of_Millipedes May 27 '26
not in C, too much foundational code expects it at this point
2
u/tux2603 May 27 '26
I would guess probably never. The number of actual use cases where length based strings have a significant advantage over null terminated strings is pretty slim, third party libraries to do just that already exist, and even without those it's next to trivial to implement length based strings on your own. Those three combined means that there's basically no pressure to add what amounts to needless clutter to the standard
2
u/ComradeGibbon May 27 '26
What you want is not length based strings but general slices.
slice char hello = "Hello"; // creates a slice of type char
slice int int123 = {1, 2, 3}; //
3
u/Radiant64 May 27 '26
Generally not a lot gets added to the standard library — it mostly remains the same POSIX subset it was codified as in 1989. I very much doubt the committee is going to spend time and effort by standardising an entirely new set of string functionality, if nothing else then simply because there is little demand for introducing something like that.
3
u/theNbomr May 27 '26
Having something in the standard isn't necessarily better. That a compiler can be most easily ported to the broadest range of architectures is one strong virtue of the C programming language. Adding significant complexity to the C language would detract from that virtue.
There is virtually no architecture that I have encountered, or even heard of, in the last 30 or 40 years that did not have a C compiler as part of its software tool set. I cannot think of a single other programming language that is expected to be supported virtually by default on every CPU architecture. C++ probably comes close nowadays but I don't think it's ubiquitous.
2
u/Daveinatx May 27 '26
The purpose of C standard is to be as minimal as possible, since C is scalable from the smallest of microcontrollers. What you're asking seems to be convenience.
2
u/Ready-Scheme-7525 May 27 '26
C strings are a fundamental datatype. They are necessary to provide interfaces for other APIs. Adding functions to manipulate C strings make sense. Adding a new datatype that would not be compatible with C strings adds nothing to the language and standard library other than convenience. It’s a great example of a standalone library.
That being said, if you’re curious, try to write one. You’ll run in to various decision points along the way. How do you encode the length? You’ll tell yourself, it’s simple I’ll just set a high bit to indicate the last byte of the length. Well, that means a length prefixed string can only be 127. Some users may not want this because all their strings will be less than 255 in length and they don’t want two byte leaders. What does your API look like? Mirror the standard library? How do you implement strtok(_r)? It will get messy quickly. You’ll learn that it is hard to standardize such things in a language like C and in the end it won’t satisfies everyone’s needs and doesn’t add anything to the language. More is not better and there are other language that offer a more complete standard library.
2
u/Keegx May 27 '26
I would imagine, more broadly speaking, that there'd be a lack of use.
Like yeah, I could see individuals and learners using it, but for groups/companies, I would guess that if they wanted custom strings, they probably would already have their own implementation for it, and wouldn't want to do a large rewrite. Same thing would probably apply if they're using regular C strings too.
1
u/WoodyTheWorker May 27 '26
I like Microsoft's reference counted CString(A/W)
1
u/LeiterHaus May 27 '26
Sounds like a struct?
1
u/Transbees May 27 '26
Wait, we're getting defer?
1
u/rb-j May 27 '26
We used to call them "Pascal" strings. But they were only one byte for the length so the string couldn't be longer than 255 chars.
I'm sure someone finally came up with a standard having a 32-bit length word in the preamble.
I wonder how modern processors do byte access to tightly pack 8-bit ASCII chars into 32-bit or 64-bit words? Some DSPs only access 32-bit words (the width of the data bus) and pointers don't point to individual bytes. Then a char* must be a different structure than that of a 32-bit unsigned int.
1
u/NoSpite4410 May 28 '26
dstrings
A type of dynamic string library for C where the allocation and data size are known, and can be used in place with all the standard libc string functions.
1
u/grok-bot May 28 '26
I was at the last WG14 meeting, they want arrays with metadata but it opens such a large can of worms that no one really wants to do it
1
u/Dangerous_Region1682 May 28 '26
Oh, I hope not.
It’s bad enough that the standards evolution try to change the language into an highly optimizable applications based programming language beyond what it was really intended to be.
The beauty of it is that it is a portable language that solved the problem of writing everything in assembler.
Occasionally adding a few helpful features over the years, the biggest being for the ANSI C standard, are welcomed, but there’s a limit beyond which C no longer becomes C. I know everyone wants optimization from the compiler but sometimes I think the standards are just going too far.
When you need more compiler flags than you can shake a stick at, to get the behavior you want, I think that things can get too far away from the original beauty and simplicity of the language.
Instead of adding evermore esoteric functionality to C, why don’t folks just transition to Go or some such.
1
u/epicalepical May 28 '26
dude, you can write a string struct. struct string8 { char *str; unsigned len; }. There. thats literally it. now just pass that struct everywhere you pass your usual char pointer strings and you're done. C is all about transparency, and ultimately a length based string is two seperate components. it doesnt make sense to make it part of a core language feature because it violates the design philosophy of C. it takes maybe 100 lines of code to write that string plus utility functions. not bad at all.
1
u/No-Archer-4713 May 28 '26
It’s C you can make any library you want
1
u/DawnOnTheEdge May 30 '26
You can use a pointer to a
structcontaining asize_tand a flexible-length array ofchar.-1
u/This_Growth2898 May 27 '26
Switch to Rust.
-1
u/iwinulose May 27 '26
No
-2
u/teleprint-me May 27 '26
Null termination is going to happen somewhere at some point. Its not a big deal. Using a function or method to get the length of the string is standard in any language.
strlen(s) vs s.length(). Whats the difference aside from function name? None. They both return the count as a number.
In C, youre working at a machine level with access to directly manipulate bytes which is a feature, not a bug.
Where the difference plays a role is when you use a buffer vs a literal.
"hello" is not the same as assigning the bytes to a buffer which must be null terminated.
Taking responsibility when the byte stream ends is part of the deal.
What would work nicely is enabling a compiler flag to catch missing terminal symbols, but this is easier said than done and could impact performance.
FilC is a perfect example of how performance overhead can affect the end result.
What youre describing is a non-trivial solution to a non-trivial problem.
-3
u/sciencekm May 27 '26
For most cases, it actually is more efficient - you only need to store the starting memory. I do this on any object that I deal with whenever possible.
Say I have an array of structures. Instead of having another value to keep track of how many items are in the array, I simply have an extra item at the end of the array and that item has a recognizable terminal value.
That's just me.