21.3 C
New York
Friday, October 7, 2022

C23 Programming For Everyone – Hackaday

Here’s a history quiz: What architecture did the first C++ compiler target? Of course, it is a trick question. The original C++ — known then as C with classes — compiler wrote out standard C code that you then compiled for whatever your target was. This has a lot of advantages since C compilers are everywhere. Now we are seeing a similar approach to bring C23 to the world with Cake. Cake can translate C23 or other versions to C99 which you can then compile with normal compilers.
While the old C++ compiler, cfront, needed special steps to compile (since it was built using C++), you can build cake for Windows or Linux easily. However, it can also be built with emscripten and you can try it yourself in your web browser.

Curious about what’s new in C23? Well, some old stuff was removed and even more was deprecated. But the really interesting things are the additions which include decimal floating point types, integers with specified bit size, standard attributes, and many changes involving constants and initialization. You can find a summary over on cppreference.com. Of course, many of these things have been around in C++ or in common extensions for compilers for a long time, but this brings a lot of common practice together in standard C.
The only other thing to watch out for is that some features are really in the library. Compiling your code isn’t going to help with differences in libraries, although many of the changes are just bringing in functions that most libraries provide any way for things like POSIX compliance.
If you don’t want to dig around for an interesting example, the drop-down box at the top of the browser “playground” lets you pick among many examples. Just press the “Compile To” button and then you can compile the output to see the program execute.
The new standard does bring some complexity, but still nothing like C++. Why use C? Lots of reasons, not the least of which is that it is energy efficient.
I miss K&R C.. It was so easy to memorize, clean and tidy. *sigh* 😔
…and have you seen B?
I’m the (main) maintainer for the only known modern cross-platform B compiler toolchain! And by ‘modern’ I mean that I ported the ABC open-source B compiler to work with the Amsterdam Compiler Kit, meaning that it’ll target anything that the ACK targets. So far there’s _working_ support for 8080, 8086, 80386, MIPS, PowerPC, and the VideoCore IV, but there’s a whole slew of other code generators which haven’t been made to work yet.
B’s a fascinatingly minimalist language. Sadly, it’s very much tied to the world where everything’s a sixteen-bit word, and it gets on very badly with 32-bit systems — B has a single data type, the word, which is used as both a pointer and an integer depending on context, and it’s defined so that adding one to a value increments the pointer to the next _word_. So it can’t address individual bytes. There are system libraries for string management that pack chars two-to-a-word. So, you’re basically SOOL if you want to access unaligned data structures on a 32-bit system.
So after the 2037 epoch bug when do we get the 2 digit year versioning wraparound bug for programming languages, 2077?
Why were these useful time functions deprecated and what is the recommended work-around? I know these functions work properly, I use them.
From: https://en.cppreference.com/w/c/23
Definition of Deprecated:
Computers. (of a software version or feature) marked as not recommended for users and developers because of the risk of damage or compromised security, the existence of superior alternatives, or an impending upgrade.
Pffft… what superior alternatives, or impending upgrade?
Read the docs? Each has another version, with _s, that is not deprecated. Same behavior, just writes to your own buffer (via standard c convention) rather than supplying it’s own. It says that the origional function is not thread safe, likely because it uses 1 global buffer for any call to that function, which is modified whenever you call it.
A lot less error prone to write to your own buffer under those circumstances.
Ah-so. I was focusing on the complex equivalents.Thank you [Nathan].
asctime_s() and ctime_s, whose big features seems to be an output buffer size specifier and output nullptr checking. These functions have been around since c11 too so they ought to be stable and proven
“These functions have been around since c11…” Didn’t know that. I need to pay closer attention to these alternatives. Thanks.
The example in the screenshot shows a try…catch statement, but I don’t see any mention of it in the linked C23 summary doc. Does anyone have more details?
Hmm. I am more interested in those compiler rules that turn the string “Hackaday!” into a string “Foo!”. Couldn’t find those rules in the docs either. 😛
Please be kind and respectful to help make the comments section excellent. (Comment Policy)
This site uses Akismet to reduce spam. Learn how your comment data is processed.
By using our website and services, you expressly agree to the placement of our performance, functionality and advertising cookies. Learn more


Related Articles


Please enter your comment!
Please enter your name here

Latest Articles