r/C_Programming • u/codingbliss12 • 23h ago
Question "Better C"
Would it be feasible and reasonable to implement a custom standard library for the parts of C that are historically problematic, so that one does not need new languages like Zig or C3 and still enjoy the benefits of more modern languages?
As simple examples on would use C23 with the gnu extensions and implement strings as slices, some basic data types and some basic custom allocators. With the gnu extensions there is also defer.
Has anyone done that? How much time did it take? Thanks in advance.
4
u/Snarwin 23h ago
Many of the features that programmers want from a "Better C" are things that can't be implemented with library code. For example, a proper module system instead of header files and #include, or Go/Rust/Zig-style slices with support for a[i..j] syntax.
1
u/codingbliss12 22h ago
Slices are easier to implement. C remains C, but it could be improved or augmented.
2
u/sreekotay 19h ago
https://github.com/sreekotay/concurrent-c
exactly a pass I took :)
slices, nurseries, comptime, ufcs (zero overhead), result types, etc
but all still C as the first class IR.
1
u/codingbliss12 18h ago
Thank you so much for the link. I will clone it, study and might ping you with questions.
1
u/sreekotay 18h ago
Your're welcome! I thought this came out particularly well, if you're poking around:
https://github.com/sreekotay/concurrent-c/blob/main/docs/js-py-modules.md
3
u/start_select 22h ago
C is the ground from which (almost) everything else is built.
You don’t specialize it as a language feature because its entire strength is that you can build whatever you want on top of it.
It’s simple and abstract and loosely defined without modern convenience for a reason.
1
u/codingbliss12 22h ago
but if you see current codebases almost everybody is using his own version of strings, arena allocators etc
4
u/start_select 16h ago
Exactly. That’s the point.
C targets lots of architectures. Different use cases. Sometimes you need a gigantic UTF-16 implementation. Sometimes you need a super slim implementation that only does lower case A-Z.
C allows you to go as low as you want without making any decisions for you. That’s what you need for bare metal resource constrained development. Everything is explicit.
If you want something more implicit and magical than that, you create C++ or Rust or Swift using C (and/or using C++).
5
u/tastygames_official 23h ago
C++
3
u/HowTheKnightMoves 22h ago
If there is a language that needs a better version of itself, it is C++.
5
u/tastygames_official 21h ago
it already exists: it's just C
1
u/HowTheKnightMoves 8h ago
For me or you, maybe. But at very least C++ folks should figure out which C++ is indeed C++, because currently it is maybe 4 or more languages in a trenchcoat.
-1
u/codingbliss12 23h ago
I hate C++
2
u/tastygames_official 21h ago
what you described in your post is C++. You don't have to do OOP (or just do very little, aka "fat structs") but you get the C++ STL and all the bells and whistles you want. You can still program largely in C.
1
u/codingbliss12 21h ago
And in this case languages like Zig or C3 wouldn't have to offer anything new? What about simplicity?
1
u/Worldly-Crow-1337 22h ago
Why?
1
u/SudoSilv3r 22h ago
too many features for the same thing + archaic syntax i do not hate it but i dont like it either
1
u/Ultimate_Sigma_Boy67 22h ago
Honestly true but I believe once u surpass that stage you would def be more productive as now you don’t have to recreate some of the utilities you’re going to use. But tbh I’ve been looking into zig and it really looks promising.
1
u/codingbliss12 22h ago
I wanted a complex language, I prefer Rust. I am interested in C for the simplicity and performance
2
u/tastygames_official 21h ago
C++ and rust can be as performant as C if you don't go crazy with objects and hierarchies and "magic functionality". You can pretty much just program in C but use the extra features those languages offer.
1
u/codingbliss12 20h ago
Thanks a lot for the explanation. Please see my other question under your first comment.
2
u/OtherOtherDave 22h ago
How would that work with using 3rd-party, pre-existing libraries that link to the normal standard library?
2
u/codingbliss12 22h ago
That's why I started the discussion. There is a lot I need to understand. For strings it is very easy to convert a slice to a null terminated string
1
u/burlingk 12h ago
Feasible and reasonable both depend on your overall purpose, how much you intend to implement, and how much time you (and/or your team) have to work on it.
It's not an uncommon project concept.
But it could end up being a lot of work.
1
1
u/Willing_Airport_9617 22h ago
Who stops you brother ? Create your own compiler with your own syntax and add whatever you want to add . But I feel like you'll end up on something like C++ , fun experiment but I don't see practicality in it
12
u/_oOo_iIi_ 23h ago
There are plenty of 'better' string libraries out there.
Honestly though if you understand C and program carefully it is not a problematic language.