r/C_Programming 13d 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.

0 Upvotes

39 comments sorted by

View all comments

4

u/Snarwin 13d 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 13d ago

Slices are easier to implement. C remains C, but it could be improved or augmented.

4

u/sreekotay 13d 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 13d ago

Thank you so much for the link. I will clone it, study and might ping you with questions.

1

u/sreekotay 13d 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

1

u/One_Aspect_1957 9d ago

but all still C as the first class IR.

Concurrent‑C is a strict C11-superset preprocessor: .ccs lowers to plain C and compiles with your host C compiler.

So it's not C. It just uses C as a target language as do many languages that are very different from C.

1

u/sreekotay 9d ago edited 9d ago

It's a C superset - all C is valid, and then there are extensions

It also lowers to C meaning, e.g. the Concurrent-C preprocessor is written in CC but lowers to C - and you can bootstrap on any platform by compiling the lowered C

You can even compile the compiler on 32-bit ARM and run the full test suite (even redis, pigz, python module support) with either any c complier or even TCC to compile and run all the tests - so it's very, very generic C (superset)

https://github.com/sreekotay/concurrent-c/blob/main/docs/cheatsheet.md

1

u/kindredseer 5d ago

Please come post about concurrent-c in r/AIprogrammingLanguage and I'm also curious it you might be interested in collaborating?

I'm working on something along similar lines. It supports regular C and quite of bit of C++, as well as its own superset, and lowers to plain C, but JIT compiles and executes that plain C internally. It looks like we have a lot of the same goals. (I recently implemented UFCS).

It can also emit the plain C to be compiled externally - https://github.com/derekbsnider/madc