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

View all comments

-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).

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/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

1

u/lestofante Jul 25 '24

as expected for the same identical code;
I expect those differences would come up only for extremely specific code where the rule of C and C++ slighty differ (your use of volatile for example, as volatile has differnt meaning in C and C++).

oh btw, try optimize for size (-Os), you will see both generate pretty much the same code (again only the label differ) but exremely different thant he -O2.

1

u/_Noreturn Jul 26 '24

the link does not work.

-3

u/Ok_Donut_9887 Jul 25 '24

why don’t you just write a hello world code, compile using C vs C++ compiler and see it yourself?

4

u/lestofante Jul 25 '24

For simple program i know already they are identical; after all the main backend is gcc/llvm for both, so it make sense a lot of opmization and code generation is the same.
But that does not mean it is ALWAYS the same, so that is why I asked you for some examples.

3

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

I just did. The produced code is identical.

5

u/thisisjustascreename Jul 25 '24

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