News for hackers. Stuff that matters.
Re:The C23 edition of Modern C is now available for free by belter on Tuesday October 15, 2024 @01:30PM (#41850897)
Important reminder just in the Preface :-)

Takeaway #1: "C and C++ are different: don’t mix them, and don’t mix them up"

Re:The C23 edition of Modern C is now available for free by jasode on Tuesday October 15, 2024 @02:38PM (#41851693)
>Takeaway #1: "C and C++ are different: don’t mix them, and don’t mix them up"

Where "mixing C/C++" is helpful:

- I "mix C in with my C++" projects because "sqlite3.c" and ffmpeg source code is written C. C++ was designed to interoperate with C code. C++ code can seamlessly add #include "sqlite3.h" unchanged.

- For my own code, I take advantage of "C++ being _mostly_ a superset of C" such as using old-style C printf in C++ instead of newer C++ cout.

Where the "C is a totally different language from C++" perspective is helpful:

- knowing that compilers can compile code in "C" or "C++" mode which has ramifications for name mangling which leads to "LINK unresolved symbol" errors.

- knowing that C99 C23 has many exceptions to "C++ is a superset of C" : https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B...

Re:The C23 edition of Modern C is now available for free by tialaramex on Tuesday October 15, 2024 @03:24PM (#41852165)
The entire I/O streams (where std::cout comes from) feature is garbage, if this was an independent development there is no way that WG21 would have taken it, the reason it's in C++ 98 and thus still here today is that it's Bjarne's baby. The reason not to take it is that it's contradictory to the "Don't use operator overloading for unrelated operations" core idea. Bjarne will insist that "actually" these operators somehow always meant streaming I/O but his evidence is basically the same library feature he's trying to justify. No other language does this, and it's not because they can't it's because it was a bad idea when it was created, it was still a bad idea in 1998, the only difference today is that C++ has a replacement.

The modern fmt-inspired std::print and std::println etc. are much nicer, preserving all the type checking but losing terrible ideas like stored format state, and localisation by default. The biggest problem is that today C++ doesn't have a way to implement this for your own types easily, Barry illustrates a comfortable way this could work in C++ 26 via reflection which on that issue closes the gap with Rust's #[derive(Debug)].

Re:The C23 edition of Modern C is now available for free by pjmlp on Tuesday October 15, 2024 @04:01PM (#41852524)
Perfectly iostreams happy user since 1993.
Re:The C23 edition of Modern C is now available for free by codr7 on Tuesday October 15, 2024 @04:44PM (#41852904)
Same, as long as I stay the hell away from locales/facets.

Type safe input/output stream types and memory backed streams served on a silver plate is a pretty decent improvement over C.

Re:The C23 edition of Modern C is now available for free by Dwedit on Tuesday October 15, 2024 @05:00PM (#41853058)
int a;

cin >> a;

Then the program goes berserk as soon as the first non-number is read out of standard input. All the other "cin >> integer" lines are immediately skipped.

Yes, I know about error checking, clearing error condition, discarding characters. But it's a whole lot of stuff you need to do after every single "cin>>" line. It makes the simplicity of cin not worth it.

Re:The C23 edition of Modern C is now available for free by eMSF on Tuesday October 15, 2024 @05:40PM (#41853403)
How could you ever continue after the second statement without checking if you actually read an integer or not? How would you know what you can do with a?
Re:The C23 edition of Modern C is now available for free by jvanderbot on Tuesday October 15, 2024 @07:57PM (#41854295)
You couldn't or wouldn't. but why have a read statement like cin>> which looks so nice and clean when you then have to go and check everything with flags and boolean casts on stateful objects.

I agree. It's lunacy. just be explicit and use functions or equivalent like literally every other language.

Re:The C23 edition of Modern C is now available for free by chongli on Wednesday October 16, 2024 @12:50AM (#41855723)
Well in a language like Haskell you could solve this with monads and do-notation. The general idiom in Haskell is to use a Maybe or Either monad to capture success/failure and you assume you’re on the happy path. Then you put the error handling at the consumer end of the pipeline when you unwrap the Maybe or Either.

I believe Rust has adopted similar idioms. I’ve heard the overall idea referred to as Railway-oriented programming.

In C++ you could implement it with exceptions, though they bring in a bunch of their own baggage that you don’t have to deal with when using monads.

Re:The C23 edition of Modern C is now available for free by tightbookkeeper on Tuesday October 15, 2024 @05:32PM (#41853339)
You’re holding it wrong. Like nan, the point is you don’t have to error check every operation.

You check error for the whole batch.

Re:The C23 edition of Modern C is now available for free by PaulDavisThe1st on Tuesday October 15, 2024 @11:46PM (#41855407)

   fscanf (STDIN, "%d", &a);
the program goes beserk as soon as the first non-number is read out of standard input.

in both cases, you need error checking (which you "know about").

Re:The C23 edition of Modern C is now available for free by unwind on Wednesday October 16, 2024 @03:54AM (#41856642)
No actual C programmer who has been around the block more than halfway should do that. The mantra is: "read into a character buffer, then parse that".

It's more code, sure, but it buys you a lot of good things. I/O is hard.

Re:The C23 edition of Modern C is now available for free by PaulDavisThe1st on Wednesday October 16, 2024 @09:37AM (#41858912)
No C++ programmer who has been around the block more than halfway should do

    cin >> a;
and assume that it works.

But also ...

    sscanf (the_buffer, "%d", &a);
doesn't help the problem in any substantive way.
Re:The C23 edition of Modern C is now available for free by tightbookkeeper on Tuesday October 15, 2024 @05:33PM (#41853344)
Yep, it’s very clean once you get the hang of it.
Re:The C23 edition of Modern C is now available for free by einpoklum on Tuesday October 15, 2024 @05:43PM (#41853427)
Then I suppose you don't care about:

* Performance

* Support for localization (as the format string and positions of values to format differ between languages).

* Code reuse & dogfooding - the data structures used in iostreams are not used elsewhere, and vice-versa

* C and OS interoperability - as you can't wrap a stream around a FILE* / file descritor

* bunch of other stuff...

iostreams work, but are rather crappy.

Re:The C23 edition of Modern C is now available for free by pjmlp on Tuesday October 15, 2024 @06:05PM (#41853587)
I care about performance, when it actually matters to acceptance testing.

The less C the merrier.

If you care about correct use of localisation, standard C and C++ libraries aren't really what you're looking for, or even C and C++ to start with.

Re:The C23 edition of Modern C is now available for free by wakawaka28 on Tuesday October 15, 2024 @07:47PM (#41854253)
C and C++ are the bedrock of operating systems with the best performance and extensive support for all languages.

The only reason why iostreams are slow is because of its incompatible buffering scheme, and the fact that C and C++ need to stay in sync when linked together. And that brand of slow is still faster than other languages, except sometimes those that delegate i/o to pure C implementations.

Re:The C23 edition of Modern C is now available for free by pjmlp on Wednesday October 16, 2024 @03:22AM (#41856448)
Historical baggage, they weren't the first system programming languages, got lucky with UNIX's license allowing for widespread adoption, and won't be the last one standing either.
Re:The C23 edition of Modern C is now available for free by wakawaka28 on Wednesday October 16, 2024 @02:35PM (#41862240)
Considering that they are evolving, I think they are more likely than not to stay standing. There might be other similar languages developed in parallel, but after over 30 years of whining C and C++ are still popular. I don't expect that to change.
Re:The C23 edition of Modern C is now available for free by throwaway2037 on Tuesday October 15, 2024 @10:53PM (#41855144)

    > If you care about correct use of localisation, standard C and C++ libraries aren't really what you're looking for, or even C and C++ to start with.
What do you recommend instead?
Re:The C23 edition of Modern C is now available for free by PaulDavisThe1st on Tuesday October 15, 2024 @11:47PM (#41855414)
_("some text") ... aka gettext and friends.
Re:The C23 edition of Modern C is now available for free by maccard on Wednesday October 16, 2024 @04:20AM (#41856784)
QT, unfortunately.
Re:The C23 edition of Modern C is now available for free by throwaway2037 on Wednesday October 16, 2024 @04:34AM (#41856862)
Why do you "unfortunately"?
Re:The C23 edition of Modern C is now available for free by maccard on Wednesday October 16, 2024 @08:31AM (#41858325)
It's like using a sledgehammer for a picture hook. I think QT is a great tool, and it solves this problem nicely, but it's a full blown framework, and if you're already using something else - particularly if it's something "light" like SDL, or just a platform specific library like Win32, it's an awful lot to pull in (plus compile times, licensing, etc).
Re:The C23 edition of Modern C is now available for free by throwaway2037 on Friday October 18, 2024 @10:16AM (#41879701)
If you are using SDL, what is wrong with GNU gettext()?
Re:The C23 edition of Modern C is now available for free by fra on Tuesday October 15, 2024 @09:05PM (#41854676)
This was a tip my hatn excellent to you
Re:The C23 edition of Modern C is now available for free by johnisgood on Tuesday October 15, 2024 @10:04PM (#41854939)
Why?
Re:The C23 edition of Modern C is now available for free by lugu on Tuesday October 15, 2024 @04:03PM (#41852543)
Thank you.
Re:The C23 edition of Modern C is now available for free by dieortin on Tuesday October 15, 2024 @05:14PM (#41853207)
> The biggest problem is that today C++ doesn't have a way to implement this for your own types easily

I’m not sure about the stdlib version, but with fmtlib you can easily implement formatters for your own types. https://fmt.dev/11.0/api/#formatting-user-defined-types

Re:The C23 edition of Modern C is now available for free by tialaramex on Tuesday October 15, 2024 @05:50PM (#41853483)
I think the problem is that your idea of "easy" is "Here's a whole bunch of C++ you could write by hand for each type" while the comparison was very literally #[derive(Debug)]. I wasn't abbreviating or referring to something else, that's literally what Rust programmers type to indicate that their type should have the obvious boilerplate implementation for this feature, in most types you're deriving other traits already, so the extra work is literally typing out the word Debug.
Re:The C23 edition of Modern C is now available for free by tightbookkeeper on Tuesday October 15, 2024 @05:35PM (#41853365)
What’s wrong with it?
Re:The C23 edition of Modern C is now available for free by wakawaka28 on Tuesday October 15, 2024 @07:43PM (#41854242)
>No other language does this, and it's not because they can't it's because it was a bad idea when it was created, it was still a bad idea in 1998, the only difference today is that C++ has a replacement.

Hindsight is 20/20, remember that. Streams are not that bad of an idea and have been working fine for decades. You haven't named a problem with it other than the fact the operators are used for other stuff in other contexts. But operator overloading is a feature of C++ so most operators, even the comma operator, can be something other than what you expect.

>The biggest problem is that today C++ doesn't have a way to implement this for your own types easily, Barry illustrates a comfortable way this could work in C++ 26 via reflection which on that issue closes the gap with Rust's #[derive(Debug)].

You can trivially implement input and output for your own types with streams.

You appear to be a Rust guy whose motive is to throw shade on C++ for things that are utterly banal and subjective issues.

Re:The C23 edition of Modern C is now available for free by PaulDavisThe1st on Tuesday October 15, 2024 @11:49PM (#41855426)
What they mean is this:

     struct Foo {
       int a;
       float b;
       std::string c;
     };


     Foo foo;
     std::cout << foo;
with no extra code. It's called reflection, where the compiler can generate good-enough code to generate a character-stream serialization of an object without any human intervention.
Re:The C23 edition of Modern C is now available for free by wakawaka28 on Wednesday October 16, 2024 @04:23AM (#41856808)
I know what reflection is of course. C++ makes it easy to implement IO. If you're asking for a reflection-based solution with less effort, you are practically asking for zero extra code. Anyway, C++ does not yet have reflection but who's to say how anyone wants any particular data to be dumped? A default implementation is nice but less useful than you make it sound. In any case, there are libraries approximating what you described (usually with macros and stuff) and reflection is totally coming at some point.
Re:The C23 edition of Modern C is now available for free by spacechild1 on Tuesday October 15, 2024 @08:14PM (#41854396)
Remember that C++ originally didn't have variadic templates, so something like std::format would have been impossible back in the day. Back in the day, std::iostream was a very neat solution for type safe string formatting. As you conceded, it also makes it very easy to integrate your own types. It was a big improvement over printf(). Historic perspective is everything.
Re:The C23 edition of Modern C is now available for free by vitaut on Wednesday October 16, 2024 @10:18AM (#41859283)
{fmt} used to support pre-C++11 compilers that didn't have variadic templates. It was a pain to emulate variadics but it wasn't impossible.
Re:The C23 edition of Modern C is now available for free by spacechild1 on Wednesday October 16, 2024 @11:59AM (#41860664)
Wow, I don't know that! I would be curious to know how they did it.
Re:The C23 edition of Modern C is now available for free by spacechild1 on Wednesday October 16, 2024 @09:38PM (#41865630)
Oh, I didn't realize you're the author! Thank you so much for fmt/std::format - it's truely a godsend!
Re:The C23 edition of Modern C is now available for free by vitaut on Monday October 21, 2024 @10:38AM (#41904652)
Glad you like it =)
Re:The C23 edition of Modern C is now available for free by vitaut on Monday October 21, 2024 @10:37AM (#41904648)
It was emulated using horrible macros: https://github.com/fmtlib/fmt/blob/8f5e07656e25ec64ec7c843f0.... IIRC some early versions of `std::tuple` did something similar.
Re:The C23 edition of Modern C is now available for free by throwaway2037 on Tuesday October 15, 2024 @10:50PM (#41855139)
Over the years, I have heard numerous complaints about C++ I/O streams. Is there a better open source replacement? Or do you recommend to use C functions for I/O?
Re:The C23 edition of Modern C is now available for free by steveklabnik on Wednesday October 16, 2024 @12:46PM (#41861176)
Re:The C23 edition of Modern C is now available for free by alexvitkov on Wednesday October 16, 2024 @01:22AM (#41855859)
> Don't use operator overloading for unrelated operations

This disn't stop with <iostream>, they keep doing it - the latest example I can think of is std::ranges operations being "piped" with |.

Re:The C23 edition of Modern C is now available for free by chipdart on Wednesday October 16, 2024 @07:59AM (#41858062)
> The entire I/O streams (where std::cout comes from) feature is garbage, if this was an independent development there is no way that WG21 would have taken it, the reason it's in C++ 98 and thus still here today is that it's Bjarne's baby.

I think this is a very lazy and somewhat conspiratorial take.

C++'s IO stream library, along with C++'s adoption of std::string, is a response to and improvement over C's standard library support for IO. That alone makes it an invaluable improvement. It's easy and very lazy to look back 30 years ago and badmouth things done back then.

It's also easy to complain about no one proposing changes when literally anyone, including you, can propose changes. The only need to do the legwork and put their money where their mouth is. The funny part is that we see frameworks putting together their own IO infrastructure and it ends up being not good, such as Qt's take on IO.

But talk is cheap and badmouthing doesn't require a pull request.

Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:20AM (#41880241)
The problem is precisely that C++ iostream library was, in practice, not an improvement on C stdio in many ways. Some of us were actually there 30 years ago, and even right after C++98 was standardized, it was pretty common for (then-)modern C++ projects to adopt all of stdlib except for iostreams (and locales/facets, another horrible wart).
Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:14AM (#41880187)
Sather (1991) used operator overloading for output. And, even more hilariously, they overloaded + in the same way as C++ overloaded <<, giving you:

   #OUT + "Hello, " + name + "!";
Re:The C23 edition of Modern C is now available for free by tialaramex on Friday October 18, 2024 @02:04PM (#41881919)
I did not know this nor indeed that Sather existed. Thanks. I don't feel as though "But Sather did it too" counts as a good reason for I/O streams, but thanks for telling me.
Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @06:51PM (#41884205)
I don't think it's a good reason, and FWIW I'm pretty sure they got the idea from C++ - iostream design predates ISO C++ by quite a bit. I remember seeing << for that with stuff like Borland C++ 3.1 that also originates in 90s, back when you had to write #include <iostream.h> etc. Just noting that this is not such an obviously bad idea that nobody else hasn't fallen into the same trap.
Re:The C23 edition of Modern C is now available for free by accelbred on Tuesday October 15, 2024 @02:52PM (#41851853)
C++ can seamlessly include C89 headers.

The C library headers for libraries I write often include C11/C99 stuff that is invalid in C++.

Even when they are in C89, they are often incorrect to include without the include being in an `extern "C"`.

Re:The C23 edition of Modern C is now available for free by nuancebydefault on Tuesday October 15, 2024 @03:03PM (#41851979)
Extern "C" around the prototypes is mandatory, otherwise your linker will search for C++ symbols, which cannot be found in the C libraries you pass it.
Re:The C23 edition of Modern C is now available for free by accelbred on Thursday October 17, 2024 @10:25PM (#41875852)
Not only prototypes but typedefs with function pointers as well.
Re:The C23 edition of Modern C is now available for free by Conscat on Tuesday October 15, 2024 @03:21PM (#41852136)
Clang supports C11 - 23 in C++, as well as some future C features like fixed-point integers. The main pain points with Clang are just the fundamental differences like void* and char, which don't typically matter much at an interoperability layer.
Re:The C23 edition of Modern C is now available for free by flohofwoe on Wednesday October 16, 2024 @02:40AM (#41856217)
There's a lot of subtle differences between 'proper' C and the C subset of C++, since C++ uses C++ semantics everywhere, even for its C subset.

Many C++ coders are oblivious to those differences (myself included before I switched from 'mainly C++' to 'mainly C') because they think that the C subset of C++ is compatible with 'proper' C, but any C code that compiles both in a C++ and C compiler is actually also a (heavily outdated) subset of the C language (so for a C coder it takes extra effort to write C++ compatible C code, and it's not great because it's a throwback to the mid-90s, C++ compatible C is potentially less safe and harder to maintain).

For instance in C++ it's illegal to take the address of an 'adhoc-constructed' function argument, like:

    sum(&(bla_t){ .a = 1, .b = 2, .c = 3, .d = 4 });
(godbolt: https://www.godbolt.org/z/r7r5rPc6K)

Interestingly, Objective-C leaves its C subset alone, so it is always automatically compatible with the latest C features without requiring a new 'ObjC standard'.

Re:The C23 edition of Modern C is now available for free by pjmlp on Wednesday October 16, 2024 @03:26AM (#41856478)
Because Objective-C initial proposal is that everything that isn't touched by Smalltalk like code, clearly in brackets or @annotarions, is plain C.

The pre-processor original compiler, before the GCC fork, would leave everything else alone, blindly copying into the generated C file.

Re:The C23 edition of Modern C is now available for free by kccqzy on Tuesday October 15, 2024 @03:31PM (#41852251)
Yeah plenty of headers first have `#ifdef __cplusplus` and then they add `extern "C"`. And of course even then they have to avoid doing things unacceptable in C++ such as using "new" as the name of a variable.

It takes a little bit of an effort to make a header work on C and C++. A lot less effort than making a single Python file work with Python 2 and 3.

Re:The C23 edition of Modern C is now available for free by flohofwoe on Wednesday October 16, 2024 @02:42AM (#41856228)
The '#ifdef __cplusplus extern "C" { }' thing only removes C++ name mangling from exported symbols, it doesn't switch the C++ language into "C mode" (unfortunately).
Re:The C23 edition of Modern C is now available for free by Someone on Tuesday October 15, 2024 @03:53PM (#41852449)
> C++ code can seamlessly add #include "sqlite3.h" unchanged.

Almost seamlessly. You have to do

  extern “C” {
    #include "sqlite3.h"
  }
(https://isocpp.org/wiki/faq/mixing-c-and-cpp#include-c-hdrs-...)
Re:The C23 edition of Modern C is now available for free by cornstalks on Tuesday October 15, 2024 @05:06PM (#41853127)
If we're nitpicking then sqlite3.h already has `#ifdef __cplusplus` and `extern "C" {`. So yes, from the user's perspective it is seamless. They do not need to play the `extern "C" {` game.
Re:The C23 edition of Modern C is now available for free by rramadass on Wednesday October 16, 2024 @01:57AM (#41856015)
Yep; I think of it as "C/C++" and not "C" and/or "C++" i.e. one "multi-paradigm" language with different sets of mix-and-match.
Re:The C23 edition of Modern C is now available for free by varjag on Wednesday October 16, 2024 @04:28AM (#41856842)
kinda like python and ruby
Re:The C23 edition of Modern C is now available for free by pjmlp on Tuesday October 15, 2024 @01:35PM (#41850960)
Specially relevant to all those folks that insist on "Coding C with a C++ compiler", instead of safer language constructs, and standard library alternatives provided by C++ during the last decades.
Re:The C23 edition of Modern C is now available for free by flohofwoe on Tuesday October 15, 2024 @02:04PM (#41851268)
Funny because for a long time the Microsoft MSVC team explicitly recommended compiling C code with a C++ compiler because they couldn't be arsed to update their C frontend for over two decades (which thankfully has changed now) ;)

https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an...

Re:The C23 edition of Modern C is now available for free by pjmlp on Tuesday October 15, 2024 @03:52PM (#41852439)
Yeah, 12 years ago, when governments couldn't care less about nation state cyberattacks, and Microsoft was yet to be called by the Congress to testify on their failures.
Re:The C23 edition of Modern C is now available for free by rdtsc on Tuesday October 15, 2024 @02:35PM (#41851659)
That thing always baffled me, this huge company building a professional IDE couldn't figure out how to ship updates to the C compiler.

> it is hard to say no to you, and I’m sorry to say it. But we have to choose a focus, and our focus is to implement (the standard) and innovate (with extensions like everyone but which we also contribute for potential standardization) in C++.

I mean, yeah if it came from a two member team at a startup, sure focus on C++, understandably. But Microsoft, what happened to "Developers! Developers! Developers!"?

Re:The C23 edition of Modern C is now available for free by Jtsummers on Tuesday October 15, 2024 @02:48PM (#41851811)
It's not baffling, it's remarkably consistent. They implemented Java as J++ and made their version incompatible in various ways with the standard so it was harder to port your code away from J++ (and later J#). They implemented things in the CSS spec almost exactly opposite the specification to lock people into IE (the dominant browser, if you have to make your site work with 2+ incompatible systems which will you focus on?). Not supporting C effectively with their tools pushed developers towards their C++ implementation, creating more lock-in opportunities.
Re:The C23 edition of Modern C is now available for free by pjmlp on Tuesday October 15, 2024 @03:54PM (#41852459)
It was on purpose, Microsoft was done with C, the official message was to move on to C++.

The change of heart was the new management, and the whole Microsoft <3 FOSS.

Re:The C23 edition of Modern C is now available for free by rdtsc on Tuesday October 15, 2024 @04:42PM (#41852886)
> It was on purpose, Microsoft was done with C

Indeed, and yet here we are with C23

> The change of heart was the new management, and the whole Microsoft <3 FOSS.

Yeah, agree. To me the turning point was when they created WSL.

Re:The C23 edition of Modern C is now available for free by kragen on Wednesday October 16, 2024 @12:46AM (#41855699)
Microsoft didn't create C23 and they don't <3 FOSS. They're accepting that they have to deal with FOSS, but installing Windows will still make your Linux system unbootable until you fix it with a rescue disk, among numerous other unfriendly things they do.
Re:The C23 edition of Modern C is now available for free by alexvitkov on Wednesday October 16, 2024 @01:36AM (#41855935)
I haven't seen Windows fuck up the EFI partition or delete the other entries in a while now. After installing it the machine will usually boot directly into it, but it should be just a toggle in the firmware to switch back to GRUB.
Re:The C23 edition of Modern C is now available for free by kragen on Wednesday October 16, 2024 @02:07AM (#41856064)
That's an improvement! When did they fix that?
Re:The C23 edition of Modern C is now available for free by chithanh on Wednesday October 16, 2024 @06:02AM (#41857343)
> installing Windows will still make your Linux system unbootable until you fix it with a rescue disk

This is no longer true. On UEFI systems the only thing you have to do normally is fix the boot order. In fact installing Linux first and Windows second tends to be the better dual-boot strategy nowadays.

Fixing the boot order can be done from UEFI setup, and even from Windows command line

  bcdedit /enum firmware
  bcdedit /set {fwbootmgr} displayorder {yourlinuxuuid} /addfirst
(Put single quotes around {} if you use PowerShell instead of CMD.exe)
Re:The C23 edition of Modern C is now available for free by kragen on Wednesday October 16, 2024 @10:18AM (#41859279)
Thank you for the correction and the howto! How long have I been wrong about this?
Re:The C23 edition of Modern C is now available for free by pjmlp on Wednesday October 16, 2024 @03:29AM (#41856495)
Microsoft doesn't take part on WG14, and MSVC only does up to C17 nowadays.
Re:The C23 edition of Modern C is now available for free by AlotOfReading on Tuesday October 15, 2024 @03:03PM (#41851984)
Funnily enough, the intellisense parser does support C syntax because it's using a commercial frontend by edison under the hood. MSVC's frontend doesn't.
Re:The C23 edition of Modern C is now available for free by com2kid on Tuesday October 15, 2024 @01:48PM (#41851082)
Perfectly valid to do if you need to interface with a large C code base and you just want to do some simple OO here and there. Especially if you cannot have runtime exceptions and the like.

This is how I managed to sneak C++ into an embedded C codebase. We even created some templates for data structures that supported static allocation at compile time.

Re:The C23 edition of Modern C is now available for free by pjmlp on Tuesday October 15, 2024 @04:00PM (#41852515)
Yeah, but one should provide C++ type safe abstractions on top.

Just like one doesn't use Typescript to keep writing plain old JavaScript, then why bother.

Re:The C23 edition of Modern C is now available for free by f1shy on Tuesday October 15, 2024 @01:53PM (#41851140)
What would be an example of "simple OO here and there" that cannot be done cleanly in plain C?
Re:The C23 edition of Modern C is now available for free by bobmcnamara on Tuesday October 15, 2024 @02:14PM (#41851407)
Templating on pixel classes so that a blitter builds all supported pixel paths separately and inlines them.

Yes you can do it less cleanly with macros or inline functions. But you can't do it performantly with struct and function pointers.

Re:The C23 edition of Modern C is now available for free by raluk on Tuesday October 15, 2024 @02:00PM (#41851225)
RAII
Re:The C23 edition of Modern C is now available for free by f1shy on Tuesday October 15, 2024 @02:02PM (#41851243)
Is RAII Object orientation? I thought it was an idiom of C++ by Stroustrup.
Re:The C23 edition of Modern C is now available for free by runevault on Tuesday October 15, 2024 @03:05PM (#41851991)
It doesn't necessarily have to be OO no. Rust uses RAII and it uses traits instead of traditional OO style inheritance etc. You do need something like destructors/drop trait for it to work as far as I know though.
Re:The C23 edition of Modern C is now available for free by tjoff on Tuesday October 15, 2024 @02:18PM (#41851451)
The killer feature of RAII is when combined with exceptions. But sneaking in exceptions in an embedded C project isn't something I'd encourage or recommend.

C++ imo doesn't offer anything compelling for the embedded usecase. Especially not considering all the footguns and politics it brings.

You can of course be strict and diligent about it but if you are you are pretty much just writing C anyway. Better to do it explicitly.

Allowing the use of the C++ standard library has been one of my biggest regrets (not that it was my decision to make, I fought it).

Re:The C23 edition of Modern C is now available for free by kccqzy on Tuesday October 15, 2024 @03:41PM (#41852332)
There are a lot of large C++ shops that purposefully disable exceptions and yet still use RAII usefully. It's so useful that in many C codebases you see people using RAII. For example Gtk has g_autoptr and g_autofree.

One of the most compelling things C++ offers to embedded use case is moving runtime initialization to compile-time initialization by liberally using constexpr functions. You literally ask the compiler to do work that would otherwise be done at runtime.

Re:The C23 edition of Modern C is now available for free by tjoff on Tuesday October 15, 2024 @04:59PM (#41853043)
RAII is useful without exceptions yes. I guess it is the other way around. Exceptions are not useful without RAII (sorry not sorry most garbage collected languages ;)).

But without exceptions it is mostly syntactic sugar anyway.

If compile time initialization is the most compelling usecase I'll rest my case. Good feature, yes! Hardly worth switching language for.

Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:29AM (#41880332)
It's very useful syntactic sugar, though.
Re:The C23 edition of Modern C is now available for free by AlotOfReading on Tuesday October 15, 2024 @03:25PM (#41852178)
C++ offers lots of compelling things for embedded use cases, like enum classes (finally fixed in C23), constexpr, std::optional, namespaces, and atomics/generics that are much smaller dumpster fires.

There's an effort to extract the good parts and make it work for embedded use cases or even bring them into C. Khalil Estelle on WG21 has been working on an experimental, deterministic runtime for exception handling, to give one example. Constexpr is an example of the latter that's now showing up in C23.

Re:The C23 edition of Modern C is now available for free by tjoff on Tuesday October 15, 2024 @03:51PM (#41852427)
I don't disagree, but these are in the ~2% convenience at most. With the huge baggage of including C++ in a project. The cost of learning C++ easily outweighs all those benefits. If you happen to have a proficient C++ team (that actually know embedded), go for it!
Re:The C23 edition of Modern C is now available for free by AlotOfReading on Tuesday October 15, 2024 @04:08PM (#41852592)
Speaking more broadly than just the std implementation, but result types like optional shouldn't be a 2% convenience, they should be used in most function calls that return errors. Rust is the obvious example here.
Re:The C23 edition of Modern C is now available for free by tjoff on Tuesday October 15, 2024 @05:08PM (#41853150)
If you argue for Rust I'm all for it, arguably much less of a learning curve than C++ too.
Re:The C23 edition of Modern C is now available for free by Ragnarork on Wednesday October 16, 2024 @09:36AM (#41858901)
Strong disagree on that one. Even though C++ has a lot of features that take a while to learn, getting started with C++ is simpler by miles than getting started with Rust.

One exception to that in my experience: dependencies, although I think that's a bit deceiving as yes, it's easier to get dependencies in Rust but in some areas they're way less mature and can sometimes be a pain to work with (usually when dealing with big C or C++ libraries that have been quickly glued to a Rust interface to be available in Rust).

Re:The C23 edition of Modern C is now available for free by tjoff on Wednesday October 16, 2024 @10:02AM (#41859146)
Agree, writing bad C++ is easy. But being competent in C++ requires much more than being competent in rust.

And not being competent in C++ is not great. You are going to be much more productive in C. The feedback loop is much faster, that is, feedback from your designs.

Contrast with Rust which is harder to get going but doesn't require nearly as much to be decent.

Re:The C23 edition of Modern C is now available for free by adamrezich on Tuesday October 15, 2024 @02:02PM (#41851244)
Namespaces, methods.
Re:The C23 edition of Modern C is now available for free by f1shy on Tuesday October 15, 2024 @02:04PM (#41851261)
Namespaces is not object orientation, is it? Am I missing something? You can place functions (methods) inside of structs in C23, can't you?
Re:The C23 edition of Modern C is now available for free by int_19h on Tuesday October 15, 2024 @02:20PM (#41851481)
You can handcode vtables in C, just as you can handcode loops in assembly (i.e. it works but it's verbose, not particularly readable, and brings more footguns).

But why would you do that if you have an instrument that lets you work at the same level as C, but with methods provided as a proper abstraction that maps exactly to what you'd have written yourself anyway?

Re:The C23 edition of Modern C is now available for free by uecker on Tuesday October 15, 2024 @03:37PM (#41852301)
I don't know, I never found the "proper abstraction" be more than irrelevant syntactic sugar. And the cost of C++ is that you end up putting everything in the header (IMHO the biggest design flaw of the language) and then compile time start to get long....
Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:26AM (#41880301)
At the very least, the proper abstraction is the one that guarantees that you pass the same pointer as the first argument of the method call as the one you used to read the vtable from.

And no, you don't have to put everything in the header with C++, and especially not if you're using it in "C with classes" mode. C++ only needs it for templates - and even then only if you want to use implicit instantiation, with `extern template` available for fine-grained control allowing you to have those specializations separately compiled and implementations kept out of the header file.

Re:The C23 edition of Modern C is now available for free by staunton on Tuesday October 15, 2024 @02:06PM (#41851286)
On a high level, "object orientation" means you think of your code as representing the state and interactions of objects. You can equally well do this in assembly. If you think of some namespace as a "singleton object" then that's what it is.

I guess what you're really asking is what are the best or most common ways to do OO in C?

Re:The C23 edition of Modern C is now available for free by f1shy on Tuesday October 15, 2024 @02:14PM (#41851403)
Oh. I learned that object orientation is primarily a way to structure data and code, such that the data is encapsulated with the code that works on it, in so called objects. So an Object is the Data, plus the functions that work on the data, an ensure that some invariants are kept. In OO parlance, that code gets executed by sending messages (calling methods).

Where can I find something about objects being "think of your code as representing the state and interactions of objects" honesty totally new to me.

So no, certainly I'm not asking ways to do OO in C. But it seems to be more definitions of object orientation as I thought...

Re:The C23 edition of Modern C is now available for free by epcoa on Tuesday October 15, 2024 @02:37PM (#41851677)
> Where can I find something about objects being "think of your code as representing the state and interactions of objects" honesty totally new to me.

I’m scratching my head how you think this is materially different than what you described in your first para. s/state/data and s/interactions/methods.

If anything though I would say the GP is more aligned with the classic definition as it highlights the focus is more on the messages (interactions) themselves rather than the implementation.

Re:The C23 edition of Modern C is now available for free by int_19h on Tuesday October 15, 2024 @02:24PM (#41851523)
There's no clear definition of what OO is, so the best you can do pragmatically is look at mainstream languages that are broadly recognized as OO and try to deduce the commonalities.

If you do that, you'll notice that, for example, encapsulation is not a part of that de facto definition, because languages like Python and (until recently) JavaScript lack it, despite being considered OO.

Indeed, the only two things that appear to be consistently present in all OO languages are: 1) some notion of object identity as distinct from object state, and 2) runtime polymorphic dispatch.

Re:The C23 edition of Modern C is now available for free by adamrezich on Tuesday October 15, 2024 @02:10PM (#41851339)
Correct, and you did ask specifically for OO things, but I thought I'd list namespaces too as far as “C++ things you might use when writing C-like C++ code”.

Another big one that I always forget C still doesn't support is function overloading.

Re:The C23 edition of Modern C is now available for free by uecker on Tuesday October 15, 2024 @03:39PM (#41852319)
Function overloading is a feature that makes code less self-documenting without providing any meaningful value. Operator overloading is more interesting, because you can build you domain language with nice syntax. But I also tend to think that this is not really worth it.
Re:The C23 edition of Modern C is now available for free by adamrezich on Tuesday October 15, 2024 @05:25PM (#41853276)
In C++ where you have methods? Sure. It would be nice to have in C, though. But, alas, ABI compatibility.
Re:The C23 edition of Modern C is now available for free by listeria on Wednesday October 16, 2024 @09:11AM (#41858696)
Function overloading enables the building of powerful idioms like swap, operator== and this [1] proposal for composable hashing to name a few. And when combined with templates (and ADL to provide some encapsulation with namespaces) you can build some interesting abstractions akin to the io module in go, like this implementation of io.ReadFull():

    template<typename R>
    constexpr ssize_t
    io::ReadFull(R reader, char *buf, size_t len)
    {
      ssize_t recvd = 0, ret;

      do {
        ret = read(reader, &buf[recvd], len - recvd);
        if (ret > 0)
          recvd += ret;
        else
          break;
      } while (recvd < len);

      return recvd;
    }
---

1: https://open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3980.h...

Re:The C23 edition of Modern C is now available for free by cozzyd on Tuesday October 15, 2024 @03:05PM (#41851998)
CRTP?
Re:The C23 edition of Modern C is now available for free by com2kid on Tuesday October 15, 2024 @07:57PM (#41854294)
You can do anything in C that you want to. Of course one can make v-tables and all of that, and even do inheritance.

But having the "class" keyword is nice. Having built in support for member functions is nice.

Sometimes a person just wants the simplicity of C++ 2003.

(In reality I was working on a project where our compiler only supported C++ 2003 and we had a UI library written in C++ 2003 and honestly pure C UI libraries kind of suck compared to just sprinkling in a bit of C++ sugar.)

Re:The C23 edition of Modern C is now available for free by throwaway2037 on Tuesday October 15, 2024 @11:14PM (#41855255)

    > You can do anything in C that you want to.
How about destructors?
Re:The C23 edition of Modern C is now available for free by com2kid on Wednesday October 16, 2024 @04:20AM (#41856788)
You can obviously build any runtime system you desire in C, including one that parses and executes C code with additional features added in. The wisdom of doing this is questionable.

Though I've actually seen macro systems that do things akin to destructors, although less automatically.

Re:The C23 edition of Modern C is now available for free by nerfnet on Friday October 18, 2024 @06:58AM (#41878209)
Surely you are aware that C++ destructors are essentially made in C.
Re:The C23 edition of Modern C is now available for free by Spivak on Tuesday October 15, 2024 @01:42PM (#41851031)
I mean as long as your goal is specifically to do that I think it's fine. Using a C++ compiler to compile a C program isn't that rare.
Re:The C23 edition of Modern C is now available for free by f1shy on Tuesday October 15, 2024 @01:56PM (#41851166)
A couple of months ago, in the company I work, there was a talk from HR, where they explained how to make a good CV (the company is firing lots of people). She say: "if you have experience in programming C, you can writing just that, or, if you have lots of experience in C, is customary to write ``C++ Experience'' "

Sooo... yeah... I should definitely change company!

Re:The C23 edition of Modern C is now available for free by varjag on Tuesday October 15, 2024 @04:09PM (#41852598)
It checks out. And if you're really good at it just write C++++ aka C#.
Re:The C23 edition of Modern C is now available for free by kstrauser on Tuesday October 15, 2024 @02:07PM (#41851294)
That literally made me do a spit take, and it was fizzy water and it burned.

My god. That's amazing.

Re:The C23 edition of Modern C is now available for free by thenipper on Tuesday October 15, 2024 @05:02PM (#41853090)
How many pluses until you should just say you have D experience?
Re:The C23 edition of Modern C is now available for free by varjag on Wednesday October 16, 2024 @04:39AM (#41856899)
Possibly three. Four pluses is naturally C#.
Re:The C23 edition of Modern C is now available for free by jpcfl on Tuesday October 15, 2024 @01:44PM (#41851047)
Bjarne should have called it ++C.
Re:The C23 edition of Modern C is now available for free by card_zero on Tuesday October 15, 2024 @02:09PM (#41851328)
Because people choose to use pre-increment by default instead of post-increment?

Why is that?

Re:The C23 edition of Modern C is now available for free by int_19h on Tuesday October 15, 2024 @02:35PM (#41851650)
It should be ++C because with C++ the value you get from the expression is the old one.

If you're asking why people use pre-increment by default instead of post-increment, it's mostly historical. The early C compilers on resource-constrained platforms such as early DOS were not good at optimization; on those, pre-increment would be reliably translated to a simple ADD or INC, whereas code for post-increment might generate an extra copy even if it wasn't actually used.

For C++ this was even worse with iterators, because now it depended on the compiler's ability to inline its implementation of postfix ++, and then prove that all the copies produced by that implementation have no side effects to optimize it to the same degree as prefix ++ could. Depending on the type of the underlying value, this may not even be possible in general.

The other reason is that all other unary operators in C are prefix rather than postfix, and mixing unary prefix with unary postfix in a single expression produces code that is easy to misunderstand. E.g. *p++ is *(p++), not (*p)++, even though the latter feels more natural, reading it left-to-right as usual. OTOH *++p vs ++*p is unambiguous.

Re:The C23 edition of Modern C is now available for free by card_zero on Tuesday October 15, 2024 @02:44PM (#41851765)
K&R seems to use pre-increment early on, then post-increment consistently (or a lot, anyway, I haven't done a thorough check) after chapter 3, in situations where either would do. In fact, after introducing post-increment at 2.8.
Re:The C23 edition of Modern C is now available for free by jpcfl on Tuesday October 15, 2024 @08:36PM (#41854531)
> It should be ++C because with C++ the value you get from the expression is the old one.

You get it!

Re:The C23 edition of Modern C is now available for free by tialaramex on Tuesday October 15, 2024 @02:27PM (#41851557)
Why use this operator? Like most C and C++ features the main reason tends to be showing off, you learned a thing (in this case that there are four extra operators here) and so you show off by using it even if it doesn't make the software easier to understand.

This is not one of those beginner -> journeyman -> expert cycles where coincidentally the way you wrote it as a beginner is identical to how an expert writes it but for a very different reason. I'd expect experts are very comfortable writing either { x = k; k += 1; } or { k += 1; x = k; } depending on which they meant and don't feel an itch to re-write these as { x = k++; } and { x = ++k; } respectively.

I'm slightly surprised none of the joke languages add equally frivolous operators. a%% to set a to the remainder after dividing a by 10, or b** to set b as two to the power b or some other silliness.

Re:The C23 edition of Modern C is now available for free by layer8 on Tuesday October 15, 2024 @03:23PM (#41852158)
The idiomatic

    void strcpy(char *s, char *t)
    {
        while (*s++ = *t++)
            ;
    }
(straight from K&R) wouldn’t work without it.
Re:The C23 edition of Modern C is now available for free by n_plus_1_acc on Tuesday October 15, 2024 @05:06PM (#41853132)
Which many people find unreadable compared to other versions.
Re:The C23 edition of Modern C is now available for free by beeforpork on Wednesday October 16, 2024 @07:24AM (#41857853)
And for several reasons.

  * is it (*s)++ or *(s++)?
  * it is not *++s nor ++*s
And I have seen

  *(*s)++
in some places!

It is concise syntax but very confusing.

Re:The C23 edition of Modern C is now available for free by chongli on Wednesday October 16, 2024 @10:54AM (#41859785)
K&R actually teaches this as a desirable idiom? People should not be recommending K&R to beginners today!
Re:The C23 edition of Modern C is now available for free by trealira on Wednesday October 16, 2024 @11:56AM (#41860622)
Kernighan spends a page whittling strcpy down to just that, with various intermediate versions. After showing you that version, he describes it like this:

Although this may seem cryptic at first sight, the notational convenience is considerable, and the idiom should be mastered, because you will see it frequently in C programs.

Re:The C23 edition of Modern C is now available for free by cozzyd on Tuesday October 15, 2024 @02:58PM (#41851920)
It's more useful for pointers than for values, IMO
Re:The C23 edition of Modern C is now available for free by trealira on Tuesday October 15, 2024 @08:54PM (#41854621)
They can be useful when adding things to an array in a loop. A trivial example which removes a character from a null terminated string:

  void remove_char(char *s, char c) {
    size_t i, j;

    for (i = j = 0; s[i] != '\0'; i++)
      if (s[i] != c)
        s[j++] = c;
    s[j] = '\0';
  }

This might be better expressed with a higher order filter function, but C is too low level for things like that.

There are also idioms for stack manipulation using them: "stack[sp++] = pushed" and "popped = stack[--sp]".

C code does a lot of incrementing and decrementing by one, and so having dedicated syntax for it is convenient.

Re:The C23 edition of Modern C is now available for free by jancsika on Wednesday October 16, 2024 @01:26AM (#41855882)
Note that in your example there appear to be three distinct meanings:

1. prefix incr/decr precedence: "stack[--sp]"

2. postfix incr/decr precedence: "s[j++]"

3. i have no particular preference for the precedence and am just using a shorthand I inherited from my ancestors whose use cases are no longer relevant to me: "i++" in your for loop

My rank speculation is that C programmers get in a habit of #3 and then forget to consider precedence in an expression where it matters.

In any case, it would be interesting to do a scan of github to see how often prefix and suffix incr/decr had to get switched up in a bugfix patch.

Re:The C23 edition of Modern C is now available for free by trealira on Thursday October 24, 2024 @08:42PM (#41941250)
Silly mistake: that should read "s[j++] = s[i]".
Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:36AM (#41880388)
For iterators, += may not even be available.
Re:The C23 edition of Modern C is now available for free by jejdjdbd on Tuesday October 15, 2024 @02:16PM (#41851427)
Why would you use post increment by default? The semantics are very particular.

Only on very rare occasions I need post increment semantics.

And in those cases I prefer to use a temporary to make the intent more clear

Re:The C23 edition of Modern C is now available for free by card_zero on Tuesday October 15, 2024 @02:25PM (#41851532)
People seem to mostly write a typical for loop ending with ; ++i){

But I write ; i++){ and seeing it the other way round throws me off for a minute, because I think, as you put it, why would you use those very particular semantics?

But I guess this is only a semantic argument.

Re:The C23 edition of Modern C is now available for free by johannes1234321 on Tuesday October 15, 2024 @02:41PM (#41851721)
> why would you use those very particular semantics?

The difference is that i++ has to keep a copy to the original around as the return value is the pre-increment value, while with ++i that isn't needed as the resulting value is being returned.

In the for loop that shouldn't matter as a) for an integer it is essentially for free (it is just reordering when the relevant register is set) and b) that value is hopefully optimized out anyways by the compiler, however as there are cases where it matters some people prefer the ++i style, some just think it looks better.

Re:The C23 edition of Modern C is now available for free by layer8 on Tuesday October 15, 2024 @03:34PM (#41852277)
In C++ the semantics can differ, in that copying an object for post-increment might require a memory allocation internally (for example in the case of a BigInt class), which may fail and throw an exception. For consistency, using pre-increment by default and unless you really need post-increment, is a good habit.
Re:The C23 edition of Modern C is now available for free by secondcoming on Tuesday October 15, 2024 @04:08PM (#41852595)
It makes no difference if the increment is done on an int, but it can make a different if your `i` is some object with its own ++ operator.
Re:The C23 edition of Modern C is now available for free by Quekid5 on Tuesday October 15, 2024 @03:52PM (#41852443)
I've seen either style, but it the argument about which is proper is pointless. Any modern compiler will optimize either equally well, unless you're doing something that actually depends on the order of the increment.
Re:The C23 edition of Modern C is now available for free by kanbankaren on Tuesday October 15, 2024 @04:14PM (#41852642)
No, for fundamental datatypes pre/post-increment doesn't matter, but for classes that overload those operators, the postfix form creates a temporary object hence people write

for(auto it = begin(v); it != end(v); ++it)

Re:The C23 edition of Modern C is now available for free by codr7 on Tuesday October 15, 2024 @04:51PM (#41852965)
If you're used to the idiom, the intent couldn't be clearer.

I miss it when switching between C/++ and other languages.

Re:The C23 edition of Modern C is now available for free by flohofwoe on Wednesday October 16, 2024 @02:48AM (#41856255)
I rarely use pre-increment tbh, but post-increment all the time for array indices (since typically the array should be indexed with the value before the increment happens).

If the pre- or post-increment behaviour isn't actually needed, I prefer `x += 1` though.

Re:The C23 edition of Modern C is now available for free by wpollock on Tuesday October 15, 2024 @05:52PM (#41853498)
The PDP-11 that C originally targeted had address modes to support the stack. Pre-increment and post-decrement therefore did not require a separate instruction; they were free. After the PDP-11 went the way of the dodo, both forms took a machine cycle so it (mostly) became a stylistic issue. (The two operators have different semantics, but the trend to avoid side-effects in expressions means that both are most often used in a single expression statement like "++x;" or "x++;", so it comes down to your preferred style.)
Re:The C23 edition of Modern C is now available for free by zabzonk on Tuesday October 15, 2024 @10:47PM (#41855124)
Please explain what you mean by "a separate instruction".
Re:The C23 edition of Modern C is now available for free by spc476 on Wednesday October 16, 2024 @12:53AM (#41855741)
Some idiomatic C code to copy a string (I'm not saying this is good C code, but it's just an example):

    while(*d++ = *s++)
      ;
 
On the Motorola 68000 (based somewhat on the PDP-11) the code would look like:

    loop:       move.b  (a0)+,d0
                move.b  d0,(a1)+
                bne     loop
 
while on the x86 line, it would be:

    loop:       mov     al,[rsi]
                mov     [rdi],al
                inc     rsi     ; extra instruction!
                inc     rdi     ; extra instruction!
                cmp     al,0
                jne     loop
 
Yes, there are better ways to write that code for both the 68K and x86, but I hope this gets the point across.
Re:The C23 edition of Modern C is now available for free by wang_li on Wednesday October 16, 2024 @11:53AM (#41860562)
> loop: move.b (a0)+,d0 move.b d0,(a1)+

...

> loop: mov al,[rsi] mov [rdi],al

This hurts my brain. When we invent time machines I'm going to use it to go back and slap whoever at intel came up with that operand order.

Re:The C23 edition of Modern C is now available for free by spc476 on Friday October 18, 2024 @02:11AM (#41876747)
It hurts less if you think of it like assignment:

    al = [rsi]
    [rdi] = al
Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:35AM (#41880372)
memcpy etc also take the destination as their first argument, and it mirrors the usual way we write assignments. Personally I always found Intel syntax to be more straightforward, but I think it's ultimately down to whatever one was exposed to first.

I wonder sometimes why we keep insisting on the "OP ARG, ARG" format in general for assembly. Why not something like `MOV X -> Y` that would make it absolutely clear and unambiguous? For that matter, why not `COPY X -> Y`, since that's what it actually does?

Re:The C23 edition of Modern C is now available for free by wpollock on Wednesday October 16, 2024 @06:47PM (#41864639)
A MOV and an INC, as opposed to just the MOV.
Re:The C23 edition of Modern C is now available for free by wnoise on Tuesday October 15, 2024 @08:20PM (#41854437)
Nah. It's just the natural semantics -- he added stuff to C, but returned something that wasn't actually more advanced...
Re:The C23 edition of Modern C is now available for free by MathMonkeyMan on Tuesday October 15, 2024 @05:12PM (#41853183)
My brief foray into microcontroller land has taught me that C and C++ are very much mixed.

It's telling that every compiler toolchain that compiles C++ also compiles C (for some definition of "C"). With compiler flags, GCC extensions, and libraries that are kinda-sorta compatible with both languages, there's no being strict about it.

_My_ code might be strict about it, but what about tinyusb? Eventually you'll have to work with a library that chokes on `--pedantic`, because much (most?) code is not written to a strict C or C++ standard, but is "C/C++" and various extensions.

Re:The C23 edition of Modern C is now available for free by rramadass on Wednesday October 16, 2024 @02:09AM (#41856074)
> because much (most?) code is not written to a strict C or C++ standard, but is "C/C++" and various extensions.

Absolutely true. I generally insist on folks learning C and C++ interoperability before diving in to all the "Modern C or C++" goodness. It helps them in understanding what actually is going on "under the hood" and makes them a better programmer/debugger.

See also the book Advanced C and C++ Compiling by Milan Stevanovic.

Re:The C23 edition of Modern C is now available for free by emmelaich on Wednesday October 16, 2024 @12:40AM (#41855660)
If you want a language with a great C FFI, C++ is hard to beat!
Re:The C23 edition of Modern C is now available for free by sim7c00 on Wednesday October 16, 2024 @04:59AM (#41857019)
can't believe so many people are arguing against this honestly. you don't mix them in the sense the author means. I take it these people didn't read the paragraphs this was the 'takeaway' from.

For example, the primary reason for the sentence seems to be from the text: "Many code examples in this book won't even compile on a c++ compiler, So we should not mix sources of both languages".

It's not at all about the ability to use c libraries in c++ projects or vice versa :S.... c'mon guys!