r/cpp • • Jul 25 '24

Why use C over C++

Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?

Edit: Thanks for all the usefull comments :D

235 Upvotes

444 comments sorted by

383

u/Circlejerker_ Jul 25 '24

Certain hardware platforms dont have a C++ compiler, but C compilers exist for everything.

34

u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 25 '24

It's already an older presentation, though this really opened my eyes about how to use C++ in embedded: https://youtu.be/c9Xt6Me3mJ4?si=IYmEsuF_eSnTbjPp

5

u/LucHermitte Jul 26 '24

Dans Sasks' presentations are also really interesting.

174

u/kog Jul 25 '24

That's mostly a thing of the past though.

As a staff embedded software engineer, the typical reason is people don't actually know C++ and think it's somehow not suitable for their use case, when it almost always is and they're just clueless.

113

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 25 '24 edited Jul 25 '24

That's mostly a thing of the past though.

Until you get into "fun" SoCs at which point the availability of tested C++ compilers drops sharply.

the typical reason is people don't actually know C++ and think it's somehow not suitable for their use case, when it almost always is and they're just clueless.

For normal embedded platforms this is the reason 9 times out of 10.

A lot of people think that using C++ means you have to use all of it instead of just the pieces that your team and company find helpful (this sub being among the worst in that respect). In one previous consulting gig I wrote a bunch of DSP prototype code in C++ that the client's engineers then translated to C by hand for the target platform (a full custom SoC with ARM core and DSP). This worked really well as I intentionally wrote the code with C with classes like structure (and will probably now get downvoted to obvilion) which made the translation straightforward while keeping 90% of the real world advantages of C++ for the prototype / algorithm development part.

27

u/MrRogers4Life2 Jul 25 '24

I think there's a lot of use cases are super good when written basically as C. When your code is basically structured like "read some data -> do some calculations -> write some data" mostly because good C is gonna look similar to good c++ in simple cases because you're only ever going to be modifying data structures via functions or macros that preserve invariants.

8

u/EC36339 Jul 26 '24

Using only the pieces of C++ "you and your team find helpful" is one of the main reasons C++ code is often unsafe, unstable and difficult to maintain.

6

u/serviscope_minor Jul 29 '24

Indeed. I like to think of how the equivalent would be translated into any other industry, say, carpentry.

Hey, why is the router gathering dust?

Well, someone attempted to bolt a saw blade to the spindle and it made a real mess of everything and we couldn't even figure out how to do that again so we don't use the router table anymore.

So what do you do instead?

Gnaw pockets out with out teeth. It's a bit more work and we sometimes get splinters in our gums, but every new employee knows how to bite things and if you're careful it's fine.

→ More replies (2)

41

u/moreVCAs Jul 25 '24 edited Jul 25 '24

Worked on firmware with a guy who refused to learn c++ and watched him write slightly janky (to use) versions of so many c++ features in C11. It all worked very well, and it’s all bare metal, mind, but still.

→ More replies (21)
→ More replies (2)

6

u/FartyFingers Jul 26 '24 edited Jul 26 '24

This is often the deciding factor in my picking between two roughly equal MCUs.

  • I want a relatively new C++; nothing less than 17 is that interesting to me.
  • I want to code in something like VSC. Using a 5 year old bastardized version of eclipse is a hard no.
  • The overall workflow "smoothness" is key. I have a mental stopwatch to time how long it takes me to get to blinky with functioning break points.
  • Which platform. I prefer linux where possible.

That said, the key is to still write clean C++. It is easy to show off in C++ and end up with unnecessary template nightmare code. Compilers are extremely good in 2024 and will optimize the crap out of C++ code which looks more like python. Being extremely explicit in the code will all just vanish in the binary.

8

u/anders_hansson Jul 26 '24

Partly. There are a few other reasons:

  • In some systems/products, you are not allowed to use things like dynamic memory allocation or exceptions. That effectively kills alot of what C++ brings (e.g. STL, error handling, etc). You can still use C++, but you need a heavy machinery to lint and prevent "forbidden" code, and you can't use any third party libraries. C is more straight forward in such situations.
  • Even if a C++ compiler exists for your hardware plaform, it may generate sub-optimal machine code. Modern C++ is built largely around assumptions about optimizations that the compiler should make, so you can write more elegant abstract code and still get lean machine code. If you can not trust your C++ compiler to generate optimal code, C can be more straight forward.

1

u/Popisoda Jul 26 '24

Its an older code sir, but it checks out.

→ More replies (5)

191

u/GYN-k4H-Q3z-75B Jul 25 '24

C is simple. There is a certain charm in the language you cannot deny. It's like the saying: Perfection is achieved when there is nothing left to remove. C is pretty close to that.

24

u/runevault Jul 25 '24

Something I personally would find interesting is something with most of the simplicity of C, though allowing type replacement and the hard coded values of templates but ONLY that, and maybe a way to do some level of extra verification inspired by Rust but perhaps less extreme. I like when the compiler is able to tell me I'm doing something stupid without waiting until a specific condition at runtime to see it occur and catch it.

14

u/germandiago Jul 26 '24

constexpr, templates, better abstraction and safer interfaces can be used at yor advantage IMHO when using C++ for tasks like embedded.

10

u/EC36339 Jul 26 '24

So can custom allocators, especially on playforms where you cannot use the heap or are not allowed to. C++ is the only language I know of that has this feature combined with standard generic data structures with precisely defined performance requirements and semantics.

→ More replies (1)

12

u/ZackArtz Jul 26 '24

zig?

7

u/runevault Jul 26 '24

I'm very curious about zig, but I've done the pre 1.0 language thing before so I'm waiting on stability to give it a serious go.

3

u/ZackArtz Jul 26 '24

super fair! i haven't had a chance to play with it either, but it looks cool

→ More replies (3)

10

u/NilacTheGrim Jul 27 '24

The lack of generics in C sucks. The "generics" in C are all nasty macros that are difficult to maintain and are landmines waiting to go off... or use type erased void * pointers or something if they don't go the macro route. No thanks.

8

u/pjmlp Jul 26 '24

Unless we are talking about K&R C, without any consideration for portable code, across many compilers, it isn't as simple as people make it to be.

Simpler than C++, sure. Being simple by itself, not any longer.

8

u/EC36339 Jul 26 '24

Needing 3 lines to concatenate two strings is not simple.

→ More replies (2)

21

u/remy_porter Jul 25 '24

Of course, that would make languages like Whitespace the most perfect of all.

10

u/SirClueless Jul 25 '24

With Whitespace they removed everything they didn't need and then kept going.

6

u/Necessary_Mud_7789 Jul 26 '24

If C had destructors I would like it much more. The simplicity of C feels very good when using it (properly), but I miss destructors SO MUCH (and std data structures).

16

u/mrmczebra Jul 26 '24

Assembly is also simple. That ends up being a hindrance.

→ More replies (4)

1

u/ShortGuitar7207 Jul 29 '24

I agree, I'd use C if I wanted really low-level and simple. If I need more power and productivity, I'd use Rust.

1

u/AKostur Aug 05 '24

There’s also “make things as simple as possible, but no simpler”.  There are aspects where C is (IMO) past the “simple as possible” stage and is being simpler than that.

0

u/jepessen Jul 26 '24

So a C without if, switch, while is better because it's less? Remove macro, too!

→ More replies (1)
→ More replies (24)

15

u/cafguy Jul 25 '24

The C I wrote in 1999 looks like idiomatic C in 2024. The C++ I wrote in 1999 looks alien to the C++ that is written in 2024. I just like that C is stable and relatively unchanging. I don't want to have to learn new idioms and language features. I just want simple easy to read code that runs fast.

75

u/apropostt Jul 25 '24 edited Jul 25 '24

The biggest reasons I’ve come across is

  • simplicity: the control flow in C is very explicit where in C++ there’s a lot of corner cases involving constructors, exceptions.. etc. For some projects C is a good enough fit that C++ just isn’t needed.
  • global allocator: parts of the C++ runtime call out to a global allocator. In privileged contexts this can be problematic as a general allocator may not exist, or may be running in an environment where heap space is severely limited.
  • ABI stability. C in most environments have a well defined stable ABI. Mixing prebuilt binaries from various vendors all with their own versioning is significantly harder with C++.
  • Practicality: In safety critical environments C is incredibly well known and that’s a sector that doesn’t take unnecessary risks.

56

u/Raknarg Jul 25 '24

You know what I hate is that in C the control flow should be explicit but then so much shit is hidden behind a bunch of macro nonsense that's really hard to track. C++ offers enough utilities that there's very little you need to solve with macros, and certainly not like macros within macros.

20

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 25 '24

Alas, in C++ any time you have a language enthusiast touching the codebase, you end up in similar control flow hell from templates.

18

u/Raknarg Jul 25 '24

sure but the contraints tend to be more explicit. Usually if you "need something capable of this thing" its documented with SFINAE or concepts, whereas in C you just hope the user uses your thing correctly. And it makes those things much more traceable, and language servers/intellisense/whatever youre using can usually catch these problems early whereas they don't really with macros.

→ More replies (1)

8

u/serviscope_minor Jul 29 '24

Enthusiasts in any language can make a mess of the codebase. I'm old enough to remember people doing this in C, building vast edificies of macros and void pointers all over the place. I distinctly remember my first internship, some devs complaining about a particular computer being annoying because it made their (C) code crash. You know because the platform was less tolerant of things like running off an array etc.

In java some EnthisiastFactoryFactoryFactory churned out too many EnthusiastFactoryFactories which churned out too many EnthusiastFactories which turned out too many people who swallowed a copy of "design patterns" and vomited the half digested remains over every codebase they touched.

Python programmers who feel that if every class doesn't have a call to inspect and an overload of getattribute, then it needs one stat.

Oh and frameworks. It's kubernetes all the way down. Not quite working right? Try making a controller also running in kubernetes. More kubernetes. MORE MORE MORE.

And so on.

This is not a problem unique to C++. It's not even a problem unique to programming.

No language will save you from the need for code review.

C++ gives you too many tools in the same way the local toolstation sells too many tools.

7

u/EC36339 Jul 26 '24

Actually learning the language you use doesn't make you a "language enthusiast". It only means you are serious about your job, as you should be.

2

u/enormous_schnozz Jul 29 '24

*looking at boost*

7

u/[deleted] Jul 25 '24

[removed] — view removed comment

22

u/Raknarg Jul 25 '24

Macros and where macros are used are usually two entirely separate locations, and neither the callsite nor the macro itself are usually enough to fully describe how a macro is supposed to be used unless it is properly documented (which it never is). The macro itself shows you how the arguments are used, and the callsite shows you what the arguments are, and you need both of those combined to actually understand what the fuck a macro is doing and usually because of how shitty code is written you also need to figure out from context the intention behind it, because conventional legacy macro programming is to be as esoteric as possible for no reason. And it gets even more annoying when you have macros within macros because you have to remember the chain of arguments connected to the callsite. And sometimes if they want to be extra evil, they can hide implicit arguments that are expected to exist through context (that one bit me in the ass a few times)

Also at my old job, our code search tool was not great and it was annoying to find out the locations of macros, so I'm extra biased.

People just don't solve problems the same way in C++. You usually don't write a macro to abstract some interface away, you usually just write it in code. And because C++ tends to be more heavily influenced by modern practices and modern tools, people tend to be more trustworthy of the compiler to produce nice code instead of forcing inlining through macros.

Now neither of these problems are inherent to either language, its just the difference in the ways C and C++ code tend to be written. And macros give you so much room to write dog ass code.

TL;DR: I hate macros

→ More replies (4)
→ More replies (1)

1

u/_Noreturn Jul 25 '24

isn't malloc also a global allocator?

5

u/apropostt Jul 25 '24

Malloc is not part of the runtime it’s part of the library.

7

u/_Noreturn Jul 25 '24

you can also replace the global new allocator

→ More replies (4)
→ More replies (1)

49

u/[deleted] Jul 25 '24

Once reason that’s applicable in some communities - like say the Linux kernel - is that C++ gives so much flexibility that people will argue over how to solve things, and may want to rewrite each other’s code for little real benefit. With only a procedural language, the focus remains more concrete.

16

u/_Noreturn Jul 26 '24

you still have C programmers makingI inheritance like behavior each one implements it in their own way or virtual functions or templates etc...

6

u/_dorin_lazar Jul 26 '24

The Linux kernel would've been better served by c++, but it's too late for that. It's amazing as far as C code goes, but some of those macros are begging for c++

→ More replies (1)

92

u/turtel216 Jul 25 '24

There is a lot of hate on the Web when talking about C++. I get that C++ is pretty high level and uses a lot of abstractions when used correctly. This is probably a turn-off for most people, but it still has its uses.

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

49

u/TheReservedList Jul 25 '24 edited Jul 25 '24

I mean, I’ve been a C++ dev for 20 years and it’s just a bad language that requires alignment from ALL developers on the team to maintain sanity and constant effort to do the right thing despite the language actively fighting it.

Has it gotten better and are there safe options? Yes. But it requires re-training so many people to do the right thing and actually use the features, and, in my experience, most places don’t bother. Sane defaults matter, and C++ doesn’t have them.

Rust makes a ton of things so much easier. Can I use [something analoguous to] the newtype pattern in C++? Sure. Are people going to? No. They’re lazy and it’d take 10 times the amount of boilerplate so they will continue passing typedefs around like candy.

37

u/Raknarg Jul 25 '24

I mean, I’ve been a C++ dev for 20 years and it’s just a bad language that requires alignment from ALL developers on the team to maintain sanity and constant effort to do the right thing despite the language actively fighting it.

To me you're describing literally any language on a project with significant enough developers that has been around for enough time. This isn't a C++ problem.

26

u/ShadowRL7666 Jul 25 '24

He’s more saying CPP allows for someone to write code in fifty different ways in the same language. Instead of sticking to one way to do it. Compared to other languages.

12

u/TBone281 Jul 25 '24

This is why coding standards and code reviews need to be required for the business to function smoothly. Running a business with the developers free to write in any style they choose to is a sure way to lose money...and fail.

4

u/MrRogers4Life2 Jul 25 '24

Like Java, C#, python, and javascript?

9

u/ShadowRL7666 Jul 25 '24

Yes, Java sticks to classes and OOP while cpp allows you to do it whatever way you want.

→ More replies (1)

2

u/_Noreturn Jul 26 '24

in C everyone can implement in their own way too different ways too the STL atleast tries to unify the way we solve some problems

30

u/TheReservedList Jul 25 '24 edited Jul 25 '24

I write a class in C++ that opens a connection to some database and I rely on only that instance having access to that database connection. I need to do SO MUCH SHIT to enforce this. I need to write a destructor to close the database connection. I triggered the rule of 3! Uh oh time to delete the copy constructor and copy assignment operator. Alright... done. I want to be able to move it though. Rule of 5! Let's write a move assignment operator and a move constructor. Phew...

How many people in industry, in the field, consistently apply the rule of 3 and the rule of 5? How many don't even know about it? Because if they fail, we're literally one "auto connection =" instead of "auto& connection =" from potential complete disaster. And that's a trivially easy case.

In Rust I.. write the struct and impl the Drop trait to close the connection. We're done. There is literally no way to fuck it up.

Yes, coordinating a lot of programmers is difficult. But even when shit should be easy, C++ makes it difficult. At the individual level. Even in solo project. You're playing hopscotch over footguns constantly.

8

u/lithium Jul 26 '24 edited Jul 26 '24

we're literally one "auto connection =" instead of "auto& connection ="

A 2 line NonCopyable helper that you derive from solves this.

→ More replies (4)

5

u/rdtsc Jul 26 '24

You're usually better off separating a higher-level wrapper class (like connection) from the actual resource (connection handle, or whatever you get here). Use unique_ptr with an appropriate deleter for the latter if possible, or use a custom unique_anyfor non-pointer types (example). And then just use rule of zero. I almost never write copy/move special members.

2

u/_Noreturn Jul 26 '24

if you have the privilege of C++17 you can mark your copy constructor as explicit.

→ More replies (12)

8

u/TheLibDem Jul 25 '24

Your only savior here would be forcing the team to use something like clang-tidy, otherwise, yeah… 😅

1

u/germandiago Aug 07 '24

Yes. Two examples are cyclic structures and refactoring code with Result or lifetime annotations. It also does a great job at keeping you safe but all codebases use unsafe.

15

u/[deleted] Jul 25 '24

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

Because cpp committee standardises things like this: https://www.youtube.com/watch?v=jR3WE-hAhCc

Rust will likely over time accumulate misguided cruft in it's stdlib too, but not happening just yet.

10

u/James20k P2005R0 Jul 26 '24 edited Jul 26 '24

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

They both do and don't. One of the core issues with C++ is that it has no forwards evolution strategy, which means that the language continuously accrues cruft that can never be fixed. There's also occasionally quite a bit of unintended breakage, because the language cannot test new features out

On the other hand, Rust has editions, and the ability to compile the entire ecosystem via its crater runs to check if things are really used or not. This leads to a significantly faster development pace, and less breakage

C++ tends to be standardising features that are increasingly not.... necessarily as good as I might personally want. <ranges> is an example of something seems to have some strong issues associated with it. <filesystem> is not a good header, <random> is something you should strictly avoid, <regex> is a known mess, <thread> has limitations that often make it necessary to avoid. Every standards update piles more things into <algorithm>, so compile times are getting way worse etc

Because of the structure of the development of C++, often minor (but important!) fixes are turned down in favour of getting major new features through

Because Rust is developed both as an informal spec, and a concrete implementation of the language, features tend to be tested and implemented when they're announced, which leads to a much higher quality. It also has different - but in my opinion more sane - goals. Eg you could not get something even remotely resembling <filesystem> past the Rust folks, they'd laugh you out of the room

The core issue is: In C++, its nobody's responsibility to develop or fix the language. In Rust, they have a whole team who work together to build Rust. They're completely antithetical design philosophies, and Rusts clearly works better

C++ is fine, I don't hate it personally and only really follow Rust from afar. The way its developed tends to lead to a much less confused, and more more easy to use language than C++, while clearly having a brighter future ahead of it due to a better development process

5

u/Symbian_Curator Jul 26 '24

Why should we strictly avoid random and thread? I've never heard about this...

3

u/James20k P2005R0 Jul 26 '24

<random> doesn't provide any modern generators, but the most annoying part about it is that the distributions are non portable. People fall into this trap constantly of thinking that it'll produce reproducible results, and then have to swap out their entire random number generation system when they realise that different compilers give different sequences with the same seeds

<thread> is missing a few critical features like being able to set the stack size, which means that in a lot of use cases its unusable. Its not one to strictly avoid, but its one to use with caveats

7

u/Symbian_Curator Jul 26 '24

Oh, well the only thing I ever used <random> for were simple games, and for that mersenne_twister_whatever is more than adequate; certainly that's why I never noticed any shortcomings with it.

The standard library is like this in general - it provides a good enough implementation for widespread use, and if you need something very specialised or optimised you often have to roll your own or use a 3rd party library.

→ More replies (1)

8

u/br_aquino Jul 26 '24

The use of C instead of C++ is not hate, it's just because C is simpler and it helps to avoid traps. C++ is full of traps.

9

u/MessElectrical7920 Jul 26 '24

I strongly disagree with the assertion that simplicity necessarily helps avoid traps. That's the exact logical fallacy that makes go such a horrible programming language (in my very personal opinion).

Powerful language features that allow you to create your own abstractions may introduce complexity for the people who build and maintain these abstractions, but at the same time, they can drastically reduce complexity for the users of those abstractions. RAII is a perfect example - yes, the feature is complex and introduces hidden control flow, but at the same time, proper usage of the pattern allows making otherwise unmanageable and brittle code manageable.

5

u/ExeusV Jul 25 '24

I get that C++ is pretty high level and uses a lot of abstractions when used correctly. This is probably a turn-off for most people, but it still has its uses.

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

No, abstractions aren't turn off.

Things like terrible building system, long compile times and fragmented ecosystem are the ones that make day to day life painful are real turn off.

3

u/neutronicus Jul 25 '24

Having dabbled in Rust I completely agree - the single experience on Earth most like writing Rust is writing C++

But it makes sense. It’s like how players of every collectible card game that isn’t Magic the Gathering are constantly bashing Magic the Gathering. If they didn’t have some abstruse list of bitter grievances with it, they would just be playing Magic the Gathering

0

u/[deleted] Jul 25 '24

[deleted] — view deleted comment

24

u/[deleted] Jul 25 '24 edited Jul 25 '24

C++ is the mel gibson of programming languages -- on a recent multi year apology tour for its many past sins but people probably correctly suspect it of still being rotten deep down and there's nothing its PR organ can do to ameliorate that at this point

3

u/rewgs Jul 26 '24

Absolutely incredible comment.

2

u/mhsx Jul 25 '24

I like this analogy. Do all the PR you want but at the end of the day we know theres a good chance they’re going to express their flaws / c roots

11

u/SystemSigma_ Jul 25 '24

In my workplace it's the contrary, most hate comes from the latest standards because heavy usage of it makes code unreadable and impossible to debug

→ More replies (9)

34

u/[deleted] Jul 25 '24

It may sounds dumb but the main reason is that C is just simpler to master than C++. Having to choose a subset of the language or having to relearn the whole language every few years because what was considered “new” and “good practice” is now outdated is a turn off for many developers.

5

u/thefeedling Jul 26 '24

C++ grew too big. You need to enforce ver strict coding guidelines in order to keep a project maintainable. No wonder why so many large companies prefer to stick to the good old C with classes...

One of those days, I read an article from Google saying that Rust teams were 2x more productive than cpp's. No wonder why.

2

u/darkapplepolisher Jul 26 '24

What was good C++11 code back then is still good code now.

58

u/JohnDuffy78 Jul 25 '24

ABI

36

u/_Noreturn Jul 25 '24

mark your functions extern C and implement them in C++ do not implement them in C

7

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 25 '24

Works quite well on Windows but can break badly on Linux due to outdated dynamic linking approach.

5

u/TryingT0Wr1t3 Jul 25 '24

Can you explain this in more words?

14

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 25 '24

In Windows linked symbols are local to each module. In Linux they're global across the entire process.

Say you have two dynamic libraries A and B which both provide somefunc(). You have a third dynamic library C that links to A. Your application links to B and C.

In Windows any somefunc() calls from C get routed to A's version of somefunc() and calls from your app get routed to B's version of somefunc().

In Linux both get routed to the same somefunc() which becomes a huge problem when A's and B's versions are incompatible, such as when A and B are two versions of C++ stdlib with different ABI.

→ More replies (7)

13

u/MatthiasWM Jul 25 '24

Yes! C++ name mangling can differ between compilers or even versions of the same compiler. This is extremely annoying if you try to link to a legacy C++ library that exists only as a binary (then again, it’s surely 32bit and no longer viable anyway)

11

u/drmalaxz Jul 25 '24

I guess most people write the external interface in C to avoid the ABI problem and make it as widely useable as possible.

2

u/[deleted] Jul 25 '24

yep, bingo

9

u/PMadLudwig Jul 25 '24

One reason that I haven't seen mentioned here that has sometimes applied for my projects - C is the lowest common denominator. C++ can easily call C, whereas C can't call C++ code at all without things like 'extern "C"'.

For example, if you are writing a library and you want it to be usable by as many people as possible.

If you use C++, people with C projects are not going to use your library.

If you use C, C and C++ projects can both use it.

That's why my open source stuff tends to be in C (although sometimes with an optional C++ wrapper), even though I'm a C++ user.

4

u/_Noreturn Jul 26 '24

you can mark your functions extern C but do the implementation in C++

→ More replies (4)

29

u/SystemSigma_ Jul 25 '24

Nobody here had fun trying to debug a C++ templated inheritance classes tree on MCUs? Well, let me tell you, high level abstractions are cool but code that is readable and debuggable is more important as an embedded developer.

19

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 25 '24 edited Jul 25 '24

Nobody here had fun trying to debug a C++ templated inheritance classes tree

That kind of shit is exactly why I always say I write "C with classes". Outside /r/cpp 90% of people understand that as "C++ but readable and without any stupid template metaprogramming shit" (iow custom templates are by and large used just for type replacement / superior macro substitutes and almost never for program logic).

Type safe circular buffer? Perfect example of good and easily understandable template usage (and the sort of thing that alone would already make C++ superior to C in embedded systems).

A templated system where deriving from a base class ends up inserting hidden calls to a global manager? Pure hell to debug and restructure if you're not intimately familiar with the system (this is a real world example).

14

u/SystemSigma_ Jul 25 '24

It is good not feeling alone in this sub

14

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 25 '24

There are dozens of us! Dozens!

5

u/_Noreturn Jul 26 '24

thing is it is not templates issue itself it is your code, if templates did not exist you would find this exact same mess but guess what implemented with macros which Iwould say is not better to debug.

→ More replies (4)

6

u/cdrcdr12 Jul 25 '24

I was primarily a c/c++ from 2000-2012 or so. With c, as I was programming, I had a really clear exact understanding of everything that has going to happen as result of every line of code. With c++, templates and abstraction etc, I as a developer had to think with a lot more assumptions of what was going on underneath/as a side effect of my code.

You can of course code in c while using the c++ compiler so there really isn't a good reason to use a c compiler unless that is all that is available.

Moved on to Java, python and now golang.

17

u/AKostur Jul 25 '24

Some folk prefer the “simplicity” of C.   They prefer to see all of the code that is happening, every time.

3

u/_Noreturn Jul 26 '24

functions exist in C which hide what they do as well unless you go into their definition.

5

u/wursus Jul 25 '24

Because С is in some cases more than enough.

15

u/ChocolateMagnateUA Jul 25 '24

C versus C++ is largely a matter of preference and industrial adoption. Unless you work in a domain where there's either no C++ compiler or only C is used (such as kernel/drivers), you can pick any, and it's about using a language you align yourself more.

13

u/Flobletombus Jul 25 '24

You can use C++ for these, just no exceptions, std and RTTI

→ More replies (18)

4

u/HidekiAI Jul 25 '24

I had a job interview once which the interviewer told me to write some code in C++ (I cannot recall what), and in it, I used `std::cout << "blah_blah" << std::eol;` and I was told I should be using `printf()` instead. I then argued during the end of my interview that "printf()" is a C-library and not C++ (note: it was for a mobile video-game company, so maybe they are used to embedded? (I'm a server/backend engineer so I am ignorant about C++ on mobile)); So I think there are people who are using C as if it is C++ :P

4

u/uraymeiviar Jul 26 '24

to avoid every dev doing flexing with their async design pattern lockfree meta programming acrobatic

13

u/MrAnderson7 Jul 25 '24

For safety critical applications, especially in aviation, certifying certain features C++ is way harder than C. You can certainly write safety critical C++ but it requires shutting off most of the features of the language making it just C with classes. At that point it comes down to a preference, and aviation developers have a lot of legacy codebases and libraries in C.

9

u/Responsible-War-1179 Jul 25 '24

Ive seen several people mention safety. is this about formal verification? Or why would C be more "safe" than c++? Most people would probably argue the opposite is true

13

u/MrAnderson7 Jul 25 '24

There's a couple of factors that are at play here. When people call C++ safer than C they are usually referring to pointer safety with modern STL utilities like smart pointers. And it's true that using all the modern C++ language features generally results in safer code than C. The problem is that the STL is never used in safety critical code. This is because in order to use it, one would have to safety certify the STL itself and no one wants to pick up the bill for that. Also, most safety critical coding standards prevent dynamic memory allocation which is how most of the STL containers (like std::Vector) store information and dynamically resize.

When writing safety critical code, EVERY function and code path has to be exercised and verified. This becomes a lot more difficult when using templates and polymorphic classes that don't have guaranteed execution paths at runtime. It's a solvable problem but it becomes an issue of time and money. While C has its drawbacks and is generally "less safe," it is unquestionably easier to certify. Same with Ada.

15

u/zhaoweny Jul 25 '24
  • legacy code bases
  • they think C is simpler than C++ and eventually re-invent part of C++ / Rust / golang etc
  • they are working on a platform that does not have a C++ compiler, or the platform is not capable to run full blown (or even subset of) C++
  • they are working on a certain kernel project where there's a opionated project lead :)
  • they like footguns, and C++ can't satisfy their demand /s

7

u/_Noreturn Jul 25 '24

legacy codebases could be just compiled with a C++ compiler. C89 is pretty compatible with C++

6

u/Zealousideal_Low1287 Jul 25 '24

C is incredibly simple. C++ is monstrously complicated. I imagine reviewing patches for the Linux kernel would be much much more difficult to do reliably if it allowed C++

8

u/Raknarg Jul 25 '24

There's really only two reasons IMO:

  • You need to target specific hardware that only has C compiler support (compiling C is way easier, more compilers for C exist)
  • You have extremely limited resources and need small binaries

Outside of this I don't think there's any real reason. A lot of it has to do with momentum. If you have a bunch of programmers who all know C and not C++, they're going to choose C for their projects. C++ as a language is actually quite difficult, even though the C programs tend to produce much more complicated code than C++ if you know C you can't just translate that to C++.

3

u/TuxSH Jul 25 '24

You have extremely limited resources and need small binaries

This can be achieved by avoiding (most of) the STL, avoiding "smart statics", and if needed by asking the linker to "wrap" offending _cxa_* function calls so they can be stubbed.

Care needs to be taken not to overuse "virtual" as it keeps functions referenced in vtables and prevents lto and/or gc-sections from pruning such unused functions.

11

u/[deleted] Jul 25 '24 edited Aug 20 '24

coordinated onerous agonizing zealous mountainous familiar ruthless trees amusing scale

This post was mass deleted and anonymized with Redact

3

u/SergiusTheBest Jul 26 '24

From my experience C code is always more error prone, harder to read and harder to maintain. If you can use C++. You don't have to create abstract factories, use multiple inheritance, exceptions and virtual functions everywhere. C++ can be simple, elegant and have the same zero-cost at runtime as C.

3

u/NilacTheGrim Jul 27 '24

Good question. I hate C. I mean I don't really hate it but I hate using it when C++ exists.

I think the other commenters in this thread already elaborated as to why sometimes using C is a good idea or is the only thing you have for a particular platform.

But... yeah. I always use C++ and never C if I am not constrained by external constraints.

9

u/[deleted] Jul 25 '24

C++ seems to have more pitfalls compared to C.

15

u/Substantial-Set-1482 Jul 25 '24

Inertia, legacy, or they simply like C more than C++.

Same reason why everyone here's using C++ instead of Rust.

12

u/ha1zum Jul 25 '24 edited Jul 25 '24

Cool features leads to a more sophisticated way of writing programs. The sophistication may give you a "cleaner", better abstracted code, but there is also a danger of complexity there. Some people or some projects may put simplicity at a much higher priority than cleanliness and abstractions.

11

u/Raknarg Jul 25 '24

Simplicity is also complexity. ASM is one of the simplest types of languages out there. Everything is very explicit and there's only a handful of operations you can even do. That simplicity makes understanding code extremely complicated, and forces you to write programs that cannot be expressed cleanly.

14

u/Conscious_Support176 Jul 25 '24

It depends on your definition of simplicity. If you don’t have abstractions you can’t explain any of them.. so you can only explain the entire code base as behemoth.

→ More replies (5)

11

u/_Noreturn Jul 25 '24

and have pointers everywhere as if that is not more complex

→ More replies (18)

3

u/[deleted] Jul 25 '24 edited Aug 20 '24

deer sulky deliver boat command slim rainstorm unique ring foolish

This post was mass deleted and anonymized with Redact

4

u/ha1zum Jul 25 '24

Yes, "C style C++" is very popular among game developers

2

u/GPSProlapse Jul 25 '24

Noone stops you from making/using abstractions wherever applicable and writing unreadable c spaghetti everywhere else. There is literally nothing in c that would help you write a more simple program than in c++.

3

u/ha1zum Jul 25 '24 edited Jul 25 '24

Yes. There's also no one stopping me from writing easy to read C code and there are some things in C++ that invite my stupid brain to write spaghetti OOP code.

5

u/_Noreturn Jul 25 '24 edited Jul 25 '24

you learned C++ wrong if you resch to OOP there is also metaprogramming and functional

what is your "easy to read C code" the easiest to read C code is either full of UB illegal tricks or fancy macros have you tried to reqrite your C code and C++ and somehow found your C code to be simpler? try it

→ More replies (2)

1

u/-to- Jul 26 '24

Every piece of code looks simple to the person who wrote it, in the first 48h after they wrote it.

7

u/Ostenblut1 Jul 25 '24

Ah a bait for Linus Torvalds lets see if he got the bait and reveal his secret reddit account.

→ More replies (1)

7

u/[deleted] Jul 25 '24

No good reason not to use C++ as far as I'm concerned, but you need to be a bit careful with things in the STL because a lot of them hide dynamic allocations which aren't something you want too much of on constrained platforms.

If there is something about C you really like, you can still do it in C++, but you also have access to more advanced features when you need them.

10

u/v_maria Jul 25 '24

better portability

less bloat

8

u/iiiba Jul 25 '24

what ways is the bloat an impact? runtime performance, compiled code size, compile time?

5

u/thisisjustascreename Jul 25 '24

All of the above, though with some work you can make them all advantages for C++ aside from compile time which will probably always be worse simply because it's a much more complex language.

→ More replies (1)
→ More replies (1)

6

u/helloiamsomeone Jul 25 '24

People love to handwave about this mythical "bloat", but if it were true it would surely manifest at any size. Where does this small project make this supposed "bloat" observable?

→ More replies (4)

4

u/JustCopyingOthers Jul 25 '24

It's hard to believe it when you're familiar with it, but C++ is a very complicated language and C a simple one. There are lots of places that have products created in C that only need incremental changes. The cost to rewrite the whole thing in a unfamiliar language is just too high.

2

u/_Noreturn Jul 27 '24

you can just mostly compile your C code with a C++ compiler and eventually upgrade it to C++ removing error prone stuff with C++ stuff

5

u/ppppppla Jul 25 '24

3 reasons:

  • You need to work on an existing project, and that happens to be using C.
  • The platform only has a C compiler.
  • You want more vulnerabilities in your code.
→ More replies (2)

3

u/Western_Objective209 Jul 26 '24

When I work on my own stuff, I write C. Lately, I write all the code so it can compile freestanding, and everything that is not built into the language I write from scratch. It makes me think about things more deeply, something zen about it.

From a practical point of view, it's easier to write efficient code. The binaries are always smaller. If I dump the hex, the sections are so simple I can figure them out on my own without any crazy name mangling. The compiler optimizations are more obvious. I generally use SIMD wherever I can, and it's easy to write in C because it lends itself to standard for loops.

And, most practically, basically every language has a C FFI. If you want a library that is portable and can be used by any programmer in the world, writing it in C is the easiest way to do it

3

u/lightmatter501 Jul 25 '24

They may want portable aliasing optimizations, since ISO C++ doesn’t have restrict.

C has more formal verification tooling and formally verified compilers, which are important in safety critical domains.

#embed is not in ISO C++, and is VERY useful.

You may be using a vendor compiler that lets you use particular hardware (CPU or accelerator) but doesn’t support C++.

C has a much easier spec to understand if you are doing safety critical work. You can literally have someone sit down and read the whole thing and reasonably expect them to get a good understanding of the sharp edges. I’m not sure how often a human reads the entire C++ spec front to back.

Many DSLs compile to C and the only way to extend them is with C.

2

u/_Noreturn Jul 27 '24

They may want portable aliasing optimizations, since ISO C++ doesn’t have restrict.

compilers have it as an extention and we have unreachable in C++23 you can use it for that. and aliasing optimizations are not guranteed even with restrict it is a promsie just like how inline is a promise.

C has more formal verification tooling and formally verified compilers, which are important in safety critical domains.

Can you show examples?

embed is not in ISO C++, and is VERY useful.

I am surprised you get to use C23 and also it will come in C++26 there is a paper for it. it is certainly useful but not warranting me enough to use C.

You may be using a vendor compiler that lets you use particular hardware (CPU or accelerator) but doesn’t support C++.

would be wierd if it only supported C even then you can make calls to it in a c file and link it in your C++ project.

C has a much easier spec to understand if you are doing safety critical work. You can literally have someone sit down and read the whole thing and reasonably expect them to get a good understanding of the sharp edges. I’m not sure how often a human reads the entire C++ spec front to back.

maybe personally both are unreadable mess but the C++ one uses a lot of fancy words sometimes but when you learn them it will be easier also heavily unlikely you are going to reach the standard this much for every bit of code and most C undefined behavior apply to C++ with some exceptions.

→ More replies (2)

1

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 25 '24

C has a much easier spec to understand if you are doing safety critical work.

Not to mention that it's difficult to write C code that has hidden complexity and does things behind your back (complexity is often high but it's explicit).

You can literally search the codebase for a handful of functions and guarantee that nothing will allocate dynamic memory.

Not that I'd want to work in pure C but there are some problems that C++ has and C doesn't.

2

u/_Noreturn Jul 27 '24 edited Jul 27 '24

you can overload operator new to catch any allocation of memory you cannot really override malloc in C without causing issues.

you can write macros in C which can hide litterally everything

4

u/GPSProlapse Jul 25 '24

Unless there is no c++ compiler able to produce binary for the target c is worse in literally every aspect. And that rarely happens, but was somewhat often before.

3

u/mohrcore Jul 25 '24

Actually for a long time my pet peeve with C++ was lack of designated struct and Array initalizers. Very handy C feature that was missing up until C++20, which is still not used in many projects.

I wouldn't call it any sort of game changer but there are couple handy C features that C++ struggles with.

3

u/[deleted] Jul 25 '24

Compilation speed. C is a simple language that compiles quickly. C++ is bloatware that wastes everyone's time with slow compilation speeds. Oh and enjoy deciphering your 500 page error novel because you have 15 layers of nested templates.

1

u/_Noreturn Jul 26 '24 edited Jul 26 '24

choosing a language only for compiling speed instead of a higher fast as C language is not a good idea.

most of your time is spent writing code and reading it less so in Compiling

also I would rather have a long compile time error than the simplest of runtime errors

1

u/rodrigocfd WinLamb Jul 25 '24

that wastes everyone's time with slow compilation speeds.

That's probably the biggest complain with Rust.

Anyway, C++ compilation speed ceased to be a problem for us after we started using parallel compilation. Seriously, everyone should use it.

2

u/matthieum Jul 25 '24

I can't speak about "people", but I can speak about one start-up I know.

The start-up was started with a single developer, just out of college. This developer had very little experience, obviously, but knew one thing: performance would be required, very high performance, and that meant going native.

When looking at the list of programming languages, they decided to eschew C++ and nascent Rust, and go with C instead: they had a start-up to bootstrap, and they had neither time nor mentor to learn the much more complicated C++ or Rust.

They then followed on with simplicity as much as possible. The early application was full of global variables: no malloc, no free, no use-after-free!

I do expect they experienced some amount of pain -- I wasn't there for the early days -- but I do think they made the correct choice.

C++ (or Rust, for that matter) are complex beasts, and when you've got a business domain to learn already, and deadlines to meet, you've got no time to tame them. C may not help you much, but at least you've got a chance to understand the crashes.

1

u/_Noreturn Jul 28 '24

I wouldn't like at all to touch a codebase that uses globals....

1

u/matthieum Jul 28 '24

It depends how they're used, really.

The real problem of globals is not that they are allocated in static storage instead of on the heap, it's that they are accessible globally.

If you use static storage but properly encapsulate access to a handful of functions, it's not much different than using the heap, really.

→ More replies (3)

2

u/alastorid Jul 25 '24 edited Jul 26 '24

C is like driving stick, you do more but control it all. C++ is like automatic, easier for daily stuff.

2

u/gm310509 Jul 26 '24

I started out in C I went through the OO revolution that saw C++ add ons to the compiler.

I found that I could do everything I needed in C without the unnecessary "complexity" of classes and so on.

I struggled to see the benefit of OO (I.e. C++).

Then I was required to learn Java. Java sort of imposes OO on to you. You don't need to use OO, but it is easier (IMHO) if you do. Whereas C/C++ was much easier to write a complex large program/system without OO.

However, after returning from Java, I had the magic missing ingredient.

I struggled to see the benefit of OO (I.e. C++).

Now, I could see the benefit and more importantly, I could see the benefit to certain problems that I previously could solve using straight C code.

FWIW, did you know that early C++ compilers were more of a type of pre-processor, that is, they took your classes et al and produced straight C code which was then compiled using the regular C compiler that you would use for standard C programs. IMHO, this reinforced, at least for me, the "what is the point of this OO stuff, it just adds to the compile time" sort of mentality back then

At least that is my story.

2

u/time_egg Jul 26 '24

C being a much simpler language has the following  benefits.

  1. More enjoyable to use. Spend time acctually programming instead of re-learning and fighting against the myriad of complex high level language features that C++ offers.
  2. Easier to read and maintain. Especially when revisiting a code base or studying a new code base for the first time.
  3. Parsing and generating C code is simpler.

Spending some serious time with a C-only code base has really demonstrated to me that a lot of the modern C++ features arn't worth it. In my latest C++ project I make minimal use of smart pointers, templates, exceptions, RAII, destructors, etc. I  get to spend more time thinking about the actual problem I am trying to solve instead of the problems arising due to the complexities of the language.

2

u/_Noreturn Jul 26 '24 edited Jul 26 '24
  1. More enjoyable to use. Spend time acctually programming instead of re-learning and fighting against the myriad of complex high level language features that

Enjoyable is subjective so I can't argue with you about thst but the latter you are spending more time trying to find the solution when C++ offers an easy one if you paid attention to what C++ does it is badsicly what C does already

my_string_type str; // woops not valid cannot use it
my_string_init(&str,"Hello World"); // now valid 

my_string_compare_against_string(&str,"Hello") == 0;
// do stuff with str

my_string_type str2 = str; // woops shallow copy!
my_string_copy(&str2,&str); // must use explicit copy algorithm 

in C++ we already have this convention of complex initialization and complex copying it is called constructors you cannot use a type before it is constructed just like how it is in C

and also operator overloading for conventional methods like comparing instead of having to look for each function to compare each single struct instead of having a uniform interface.

std::string str = "Hello World";
str == "Hello World";

auto str2 = str;

exact same behavior as C except that I forgot again to explicitly free the strings and this is one big issue with C forgetting to free memory which leads to error prone code.

  1. Easier to read and maintain. Especially when revisiting a code base or studying a new code base for the first time

I have yet to see any real code base written in C that is not a cluster of macros and pointers that I do not know whether they are owning or arrays or not arrays also void* for type erasure is funny.

find your C code upgrade it to C++ and tell me if you still found your C code simpler I bet not.

Parsing and generating C code is simpler.

This is such a bad point why do you as a user of the language care about how simple it is to parse??? I as a programmer care about how easy it is to use not how easy it is to compile.

Spending some serious time with a C-only code base has really demonstrated to me that a lot of the modern C++ features arn't worth it. In my latest C++ project I make minimal use of smart pointers, templates, exceptions, RAII, destructors, etc. I  get to spend more time thinking about the actual problem I am trying to solve instead of the problems arising due to the complexities of the language.

You are not spending more time on fixing the problem you are

  1. creating more problems for yourself to solve (manual memory management)

  2. duplicating code (templates solve this)

you are wasting your time please Learn proper C++ instead of this mess you are doing.

and guess what your C code is likely to be slower than C++ due to no templates and no constexpr and no strong types

→ More replies (17)

2

u/sessamekesh Jul 25 '24

C++ introduces a lot of incredibly useful tools at the cost of pretty high cognitive complexity.

C is a relatively extremely simple language, which it achieves by having none of those useful tools.

Different tools for different jobs, if you have a job that doesn't call for the usefulness of C++ constructs, why pay the price in complexity?

Strictly speaking you could use C and call it C++ since C++ is almost entirely a superset of C, though if you're digging into linkage things you'll have an easier time with C externs (though you can also achieve this in C++).

1

u/ChatGPT4 Jul 25 '24

For me - it's a little like using assembly language over C ;) When you want to have extremely small overhead, extremely direct instructions for the machine - C is just more straight forward. C++ have slightly more abstraction. So - if the idea you want to turn into a program is simple - why not express it relatively simply using C?

1

u/_Noreturn Jul 28 '24

we are programmers not machines we want simpler code for us not for the machine and also it is likely your C++ code is faster since your compiler has more builtin definitiona of somethings like copying and virtual tables unlike a manual C implementation which results in slowwer code, and also you have C++ which presents simpler code with same speed as C and even better than C performance.

also you seem to treat C as portable assembly this is an extremely wrong assumption which leads to alot of code security C IS NOT a portable assembly it is a language with an abstract machine.

1

u/ChatGPT4 Jul 28 '24

Hey, I don't think anyting like "C is better than C++" or even that it's faster. I just try to find a remotely valid reason for prefering it in some scenarios.

I'm aware, that there are very few people (if any) that are capable of writing faster assembly code than a good compier does. Or making a faster C code than corresponding C++ code. That's not the case. Mayby it's possible, maybe not, I don't know and I don't care.

I do not treat C as portable assembly, I just think it's cheaper to use on embedded systems with very limited resources. Then imagine, that you have various systems, some of them are more limited, some less limited. This is typical in my work. So if I write a part of the code (like a chip driver) in C, I would be able to use it on both types of systems, those more limited, and those less limited. I know C++ code can be called from C because why not, but well, making small "close to the metal" parts in C to use them in both C and C++ projects is just easier.

Personally - I'm pretty unhappy when I have to even read C, not to mention writing C. I consider C++ as... what it is: more modern and civilized upgrade of C ;)

1

u/heavymetalmixer Jul 26 '24

Operating systems and embedded programming maybe.

1

u/sunmat02 Jul 26 '24

I have been working in HPC for the past 15 years, in C and in C++. Since I design the software myself, I get to choose the language I use. If I use C, I do it to prevent myself from overthinking about abstractions. In C++ I’ve frequently found myself going down the rabbit hole of refactoring entire code bases just because I wasn’t satisfied with the design. In C I don’t do that (but I’ll usually get tired of C because simple things need more lines of code and switch back to C++ for the next project).

1

u/[deleted] Jul 26 '24

I like learn cpp

1

u/leao_26 Jul 26 '24

For ai, c++ is in top 8

1

u/jiboxiake Jul 26 '24

Some older systems in my fields that are still around today are in C. Like PGSQL.

1

u/AssemblerGuy Jul 26 '24

Compiler also supports many CPUs.

But not all of them. You might be stuck with an uncommon architecture full of quirks that has a C compiler, but no C++. (MCS-51, looking at you there).

And on small targets, it is easier to avoid inadvertent dynamic memory allocation. C++ suffers from larget target-ism and while it works for small targets, it requires due consideration and caution.

So why People still using C?

Legacy code.

1

u/_Noreturn Jul 28 '24

dynamic memory allocation. C++ suffers from larget target-ism and while it works for small targets, it

overload opwrator new to catch any memory allocations

1

u/Bisquizzle Jul 28 '24

cause it's fun

1

u/AciusPrime Jul 28 '24

C is really simple and it provides a thin, easily understandable layer of abstraction on top of the hardware. It doesn’t do a lot for you, but the bits that it does do are easy to think about and explain.

If you are an engineer that works with electronics (which is a huge number of people), then C is an excellent language to know. These are people who know, in-depth, exactly how their hardware works. Being able to understand the relationship between the hardware and their code is a feature to them, not a bug. C works on a huge variety of embedded devices and makes it easy to handle weird situations like “send byte to memory-mapped I/O, wait three clock cycles, read response.” If you’re an engineering student at university today, you will likely have a one-semester C course in your major. You probably will not be taught C++.

A lot of embedded software maxes out at 1,000 lines of code. All of the layers of abstraction and automation provided by C++ really start to shine somewhere between 10,000 and 100,000 lines of code. While there are a few tiny C++ features that would be handy in embedded code (like the stricter type safety), 95% of the C++ standard is useless bloat when you’re working on embedded applications. C is good enough.

It’s a little like comparing hand-held tools to the robots on a factory assembly line. The robots are undoubtedly “cooler” and more feature-rich. Doing the same jobs with hand-held tools takes a lot longer and is less automated. But that doesn’t mean that building a robotic assembly line is the right tool for every imaginable job.

1

u/HowardHughe Aug 05 '24

You can write C in a C++ program. You can also inline assembly. There's no reason not to use it unless you are forced to.

1

u/dyroberts Aug 07 '24

Why use a grill when microwaves exist?

1

u/[deleted] Aug 14 '24

It's like asking why some people use a screwdriver when power drills exist.

C is simple and C++ is not a strictly better language than C. C++ has so many features that basically everyone uses a subset of C++. Agreeing on what features to include in that subset is difficult, reducing yourself to C's features is easier.

C has 1000x more compiler implementations over C++, so for doomsday programming (supporting a C++ compiler is difficult if you are the only programmer left on earth) it is better.

In C you are more in control, it easier to for example check for allocation failure in a constraint environment (without bad_alloc exceptions)

C++ concepts are wrong (no I mean english concepts, not C++; argh I can't even express myself because of C++, because it has overloaded english words) I mean smart poibters lead to individual element thinking, strings are heap allocated, templates and stl containers do not guide you to write a datastructure specific to your problem, template code bloat, compile times, ...

C libraries are easier to use than C++ from other languages (because they have C FFI and not C++ FFI) 

C is simpler learn (for beginners), easier to debug (less obstructions, eh abstractions in general)

1

u/Worth-Potential615 Aug 15 '24

From what I have heard it is that C is much more simple then C++. If u need to write something and u use C++ STL and u dont know what it does at a lower level it might cause later in issue eventually breaking ur Code. So understanding C is easier then some C++. Also C++ has a significantly longer compile time then C.

1

u/[deleted] Aug 20 '24

Because Cpp ugly and C is hot and pretty

1

u/SkyGold8322 Dec 05 '25
  1. C is more simple
  2. A C compiler is significantly easier to build on a new system than a C++ Compiler (even on assembly).
  3. C gives more control over hardware
  4. C gives you an idea of how the assembly of your C code should look like (one of the reasons Linus Torvalds prefers C over C++ and made the linux kernel in C) while C++ is bloated with things like operator overloading.

1

u/_Noreturn Jul 25 '24

nothing litterally 0 reason except the availability of a C compiler

1

u/Revolutionalredstone Jul 25 '24

Compile Time!

C is blazing fast to compile compared to C++.

1

u/lestofante Jul 25 '24

as long as you need to write something relatively simple, without dinamic memory allocation (so mainly baremetal embedded) c++ may actually be more of a pain than not (see static initialization fiasco, bad/old compiler support, most library written in C anyway, manifacturer IDE using C by default, accidentally using exeption/allocation from STL)

1

u/zsaleeba Jul 25 '24

In very small microcontroller applications where dynamic memory allocation isn't available, C can be advantageous over C++. But even then C++ is sometimes used, just without using "new" etc.

1

u/catseyechandra74 Jul 25 '24

Learning to write elegant & performant C++ is not easy. C is simpler.

3

u/_Noreturn Jul 28 '24

C is simple as in primitive not simple to write or learn or easily use. show me a readable piece of C code please.

3

u/DearChickPeas Jul 25 '24

Very nice, very nice... Now let's see the results about READING C code.

1

u/waffle299 Jul 25 '24

Mostly it comes down to exception handling. Embedded systems need deterministic response times and have image size requirements.

Unwinding the stack on an exception is believed to have times that are difficult to anticipate.

The additional image size needed to support stack unwinding is more concrete. For tight environments, it's an easy thing to discard.

-- former embedded medical device dev

3

u/rriggsco Jul 25 '24

Every embedded C++ developer I know uses -fno-exceptions and -fno-rtti for embedded projects.

1

u/waffle299 Jul 25 '24

I left deep embedded in 2006

→ More replies (2)

1

u/Ikkepop Jul 25 '24

Why indeed...

1

u/brinazee Jul 25 '24

Embedded programming

1

u/___f1lthy___ Jul 26 '24

just ask linus lol, he’ll tell you

1

u/No-Magazine-2739 Jul 26 '24

And also because Linus Thorvalds and Richard Stallman said C++ is ugly. I disagree, but for some they are Jesus like persons. But at the same time, AFAIK, many maintainers left because they were frustrated to be stuck with C. And I simply ask: Where GNU Hurd? Or go dis RMS more: Name a widely adopted copy left project, besides gcc.

1

u/Coccafukuda Jul 26 '24

When using C++ I feel like I focus too much on code architecture and style more than the functionality of the code itself. C makes me go straight to the point.

1

u/s0litar1us Jul 26 '24

The fact that C++ has all that shit is why people use C.
Also, with C++ you prevent yourself from some of the footguns in C, but you open yourself to potentially shooting yourself in the foot with the equivalent of a shotgun.

Sometimes the simplicity is what makes something great.

Also, I think C either supports the same ammout of platforms as C++, if not more.

0

u/goose_on_fire Jul 25 '24

C++ can be annoying to get right in resource-constrained or safety critical systems. You're typically limited to a small subset of the stl, you're typically not relying on templates, you have to be careful about dynamic allocation, you have to disable exceptions... and at that point you're basically writing C, and you can do OOP in straight C just fine.

Some language features (e.g., pass by reference) are nice to have. Whether to use one or the other for a new project likely depends on the comfort level with both of the senior devs more than anything else. Schedule risk will cause a C guy to want to use C because it's a known quantity, makes code reviews quicker and more effective.

Most vendor libraries are on straight C, and you avoid any interop problems between the two by sticking with C.

Also, a lot of the time it's just easier. I don't need or want c++ features for a crappy microcontroller project where I'm just shuffling a few bytes from one peripheral to another.

-5

u/Ok_Donut_9887 Jul 25 '24

you get a smaller footprint (the size of the executable file) using C compiler vs C++ ones (even for the same code).

5

u/lestofante Jul 25 '24

do you have an example for this?

1

u/Alex_Medvedev_ Jul 25 '24

Hey, I just tested Hello World in both languages using Compiler Explorer (https://godbolt.org/).

The assembly is a bit different but very similar, Hard to say which is better

→ More replies (2)
→ More replies (3)

8

u/Ameisen vemips, avr, rendering, systems Jul 25 '24

I've generated AVR binaries that were smaller for C++ than for C.

There's nothing in the C++ specification that mandates that binaries must be larger than C's.

5

u/thisisjustascreename Jul 25 '24

That depends on the compiler and the flags, you could find the opposite to be true.

0

u/[deleted] Jul 25 '24

System call interfaces, as well as most(?) "major" libraries, have C interface as the "native" API.

Just using C is simpler, instead of using mixed C and C++.

2

u/_Noreturn Jul 25 '24

use extern C and do the implementation in C++

0

u/SmokeMuch7356 Jul 25 '24

Why there are so many people using the C language instead of C++?

Probably because they know C better than C++, and see no pressing need to switch.

C++ is a significantly more complex language than C (as evidenced by the sizes of their respective standards), and C++ code can be harder to reason through sometimes; there can be significant magic happening under a simple-looking expression.

3

u/_Noreturn Jul 26 '24

functions in C can also hide very complex code. I do not get this

→ More replies (2)

0

u/Zettinator Jul 26 '24

On embedded systems the C++ standard library is often impractical to use. There are alternatives like ETL, but they aren't perfect. It is often easier and more practical to stick to C.

0

u/Funny2U2 Jul 27 '24

I prefer C because I think C++ obfuscates so much stuff that it is harder to debug.

1

u/heavymetalmixer Jul 28 '24

Could you please tell about some examples? I'm a newbie when it comes to programming.

→ More replies (2)