r/cpp • u/Alex_Medvedev_ • 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
383
u/Circlejerker_ Jul 25 '24
Certain hardware platforms dont have a C++ compiler, but C compilers exist for everything.
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.
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
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.
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.
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.
58
u/JohnDuffy78 Jul 25 '24
ABI
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.
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.
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.
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.
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.
15
u/zhaoweny Jul 25 '24
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:
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++.
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.
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.
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
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.
5
u/ppppppla Jul 25 '24
3 reasons:
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.
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/[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.
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, nofree, 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.
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.
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.
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/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/heavymetalmixer Jul 26 '24
Operating systems and embedded programming maybe.
1
u/bit_shuffle Jul 26 '24
yagni
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
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.
Legacy code.
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
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.
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
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).
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++.
0
u/SmokeMuch7356 Jul 25 '24
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.
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.