DEV Community

Cover image for What to choose: C vs C++?

What to choose: C vs C++?

Heather Parker on March 24, 2023

C vs. C++ is a popular developer blog topic. C and C++ are programming languages that can be used to create games, GUI applications, operating syst...
Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

"Well written code in any language is always better than poorly written code in any other language."

As a conclusion, use the one you feel more comfortable with.


That being said, and as a more detailed response, C is used in most places whenever you need a small executable code size, where C does have a slight edge.
Judicious use of C++ still allows it to be used in embedded applications, but it is less popular due to fear that unwanted language features will creep in.

Generally speaking (nuances and use-cases can lead to differ on this conclusion), the memory footprint of C is also lower (which is better). A more accurate way of stating the same would be "Generally speaking, most of the time you use C++ specific features (OOP and some abstraction layers) it will make your software a biiiit more memory consuming and a biiit less performant than it would be in plain C".

(Another question being if the usually-super-tiny difference really makes any difference in your context).

Both are great languages and they have different niches (even though lots of times they both overlap in the same niche). The preference for one or the other should be the result of the analysis on:
1- Market niche you're interested in.
2- Popularity of one or the other in that given niche.
3- Open positions on that given niche regarding each language.
4- Trends and tendency on the market for that given languages on the given niche.

In the end, we people like what we understand and know so I'll call learning and understanding the one better suits your needs or your career projection a wise move 😁

Best regards!

Collapse
 
trpricesoftware profile image
Taylor R Price

First off, I 100% agree with your first statement. Use the language in which you're comfortable writing good code.

My inclination re: C vs C++ is the following:

  1. C++, once you've learned what is available in the standard library (and others), will allow you to use abstractions that make the code MUCH easier to read than they might otherwise in C. That's not to say there aren't abstractions you can use in C but there are more and, arguably, better in C++.
  2. C, on the other hand, doesn't try to hide the fact that you're going to deal with raw memory manipulation and all. So, if you're going to do that for performance sake, use C and make it obvious. There are lots of ways to write good code in C.
  3. You may also be constrained to writing C by the API or computer you're working on.
Collapse
 
swordheath profile image
Heather Parker

I loved to read your thoughts!

Collapse
 
jimmymcbride profile image
Jimmy McBride

Why not use Rust over both?

Collapse
 
swordheath profile image
Heather Parker

Rust is a great language without any doubt, but to me personally, C++ was easier to learn (as well as C)

Collapse
 
rediedotie profile image
Redie Dotie

In what way is Rust better than C and C++?

Collapse
 
jimmymcbride profile image
Jimmy McBride

Here, I wrote a little blog about the differences. dev.to/jimmymcbride/rust-vs-c-and-...

Thread Thread
 
swordheath profile image
Heather Parker

Great blog btw!

Collapse
 
mrdgh2821 profile image
Mihir Rabade

Incorrect:

Compatibility (Code written in C can be run on a C++ compiler because C is the foundational language; code written in C++ can be run on a C compiler because C++ includes the concept of OOP);

Should be changed to:

Compatibility (Code written in C can be run on a C++ compiler because C is the foundational language; code written in C++ can not be run on a C compiler because C++ includes the concept of OOP);


By the way, I learnt C++ before C, so it was a breeze for me to learn C.
Frankly speaking C++ would be good choice to learn first as its God's Programming Language Enhanced.
Syntax similarity was the key factor in making it easy to learn C after C++

Collapse
 
swordheath profile image
Heather Parker

Thank you so much for correcting this point!

Collapse
 
leob profile image
leob • Edited

Learn C, and then you can always learn C++ "on top of it" later on, as C is an almost pure subset of C++, so you lose nothing ... C++ might feel overwhelming for a beginner, start with C, then expand into C++.

(with C++ I've always had a "designed by committee" feeling, C has the elegance of simplicity ... OTOH, I think C++ is used way more often these days, if you want to do anything practical with C/C++ then you'll want to know C++)

Other options - look at Go or at Rust (Rust is closer to C/C++ as it has no garbage collector, but Rust isn't exactly easy to pick up for a beginner).

Oh, first things last, lol - I completely forgot to ask what your goal is or what you want to use it for - systems programming, embedded programming, web even (WASM) ... ?

Collapse
 
swordheath profile image
Heather Parker

That's true! Regarding your question - I use C++ for systems programming mostly)

Collapse
 
efpage profile image
Eckehard

What a strange discussion. How do you learn C++ without C? As you mentioned:

C++ is a superset of the C programming language; C++ can run 99% of C code

So, You need to learn C first before you learn C++. You will not be able to write any C++ code without a good knowledge of C. It is also perfectly ok to write procedural C-code in C++ without classes.

What are "classes" in C++? Classes contain methods and properties, but this is just a different naming for procedures and variables:

  • a method is just a procedure, that lives inside an class.
  • a property is just a variable, that lives inside a class. So, an object is very similar to a small independent program, that lives inside a larger program.

If you are not used to this terms, what is the difference of a class and an object? A class is just a template for an object. This gives you the option to have multiple objects of the same type, each having it´s own identity (-> set of variables). This is the base of "reusability"

So, you can decide to learn C. After that, you can decide to learn C++ too, but not vice versa.

Class based OOP (There are in fact other approaches too) was created to reduce the complexity of the code, but in the core all code is procedural code, even if you use Classes. Often enough you start to write some procedural code. If your code grows, OOP may help you to organize your code.

Or you want to make your "procedural spaghetti" reusable. So you will insulate parts of your code inside a class. As a class is completely insulated from the rest of your code, this will help you to findy any unwanted dependencies.

Often enough it is hard work to create a well designed class hierarchy. So, for a small program, it might be overkill to use classes at all. But for big code monster, that has been refractored again and again, this might be your life saver.

Collapse
 
swordheath profile image
Heather Parker

That's true, but I believe that such discussions (even though they might seem weird) can help the developer community dive deeper into the understanding of programming language differences and maybe find out more about them from the perspective of different people

Collapse
 
sloan profile image
Sloan the DEV Moderator

Hey, this article seems like it may have been generated with the assistance of ChatGPT.

We allow our community members to use AI assistance when writing articles as long as they abide by our guidelines. Could you review the guidelines and edit your post to add a disclaimer?

Collapse
 
swordheath profile image
Heather Parker

Hello! I don't understand why there is a suspicion that ChatGPT was used, because that's not true. I understand that now, with the high popularity of this AI tool, everyone is under suspicion, but not all authors use this method. With all due respect to you and the entire dev.to community, I don't think that I must edit my post only based on suspicions.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Wow Sloan, do we have checkers on that? That's amazing, tell me more! 😁

Collapse
 
ozzadar profile image
Paul Mauviel

I came down seeing if anyone else felt that this article was AI generated.

Collapse
 
sweetpapa profile image
Forrester Terry

It was nice to learn C++ and JavaScript first... it helped me really appreciate what object oriented programming offers out of the box. Jumping into C I was like "How do I... ohh, right..." haha.

That said, most of the time when I use C it is out of necessity or because I am working on some sort of embedded device that calls for it. Otherwise, C++ would be my preference -- or, I would likely use something like GO or Rust instead if I am building something for a desktop app or backend service.

Collapse
 
swordheath profile image
Heather Parker

Couldn't agree more, I also prefer C++ a little bit more than C

Collapse
 
cubiclesocial profile image
cubiclesocial

You can write C applications in C++. As you noted, there's quite a bit of crossover.

C++ has two major features that are somewhere between difficult to impossible to implement in C: Virtual tables and template functions. You can hack some stuff together to accomplish similar objectives in C with macros, structs, and typedefs but it gets ugly very quickly. Some languages like PHP are written in C and essentially have devolved into "everything is a macro" in many cases where C++ vtables and templates would have been a much cleaner solution.

The C++ compiler is generally more strict about typing than C. It catches a lot of assignment errors with mismatched data types that would otherwise result in application crashes that C will happily compile. Debugging a crash bug at runtime that could have been caught at compile time wastes a lot of time. So even if you decide to write C code, I recommend running it through a C++ compiler just to catch the easy stuff.

The macro preprocessor in C/C++ isn't all that great. For example, a simple compile-time incrementing variable requires abusing #include (see Boost) because there's nothing built into the language itself.

As to security, both C and C++ are not really secure. Buffer overflows are the biggest risk a developer takes on when working in C/C++ and are also the most common security vulnerability. Buffer overflows can lead to unexpected code execution. There are various mitigations in place to prevent buffer overflows from being catastrophic but it is better to just take care to not step outside memory bounds in the first place.

In my opinion, you need to have a reason to write C/C++ code or you'll come away from the experience with a bad taste in your mouth. For example, notably improved performance, direct access to system calls or hardware, and using third party libraries written in C/C++ are good reasons to interface at the C/C++ level. Higher level languages like PHP and Python can be extended by writing C code - usually to bring in a C/C++ library and expose it to the userland layer or to expose a system call.

In my experience, it takes about 6 times longer to build anything significant in C/C++ than in higher level languages. Ideally, I prefer to prototype something in a higher level language like PHP first before delving into C/C++ but that is not always possible. A prototype in another language can work out a lot of missteps and save a ton of time when porting to C/C++ later because you have a working example to create the port from. If it would take 6 months to write something in C/C++, writing it first in PHP takes 1 month and porting the PHP code to C/C++ takes another few weeks. Total time taken is 2 months instead of 6 months. It's all about working smarter, not harder!

As to Rust, it may be the new kid on the system language block but C/C++ has hundreds of millions of lines of code, maybe even billions of lines, that have been tested and tempered by time. New languages pop up every few years and seem to ignore the existing body of work and thus starts again from ground zero. Rust attempts to bypass the buffer overflow tendencies of C/C++ code but every language has its issues. I think you also need a reason to write Rust code other than "it's new and shiny." There's a lot more C/C++ code out there than Rust code.

Collapse
 
swordheath profile image
Heather Parker

Such a great comment!

Collapse
 
grunk profile image
Olivier

Honestly today i will use C only in embedded environment. In any other context C++ features are a no brainer.

I don't use C/C++ in my new job but, today if i had to start a new project i'd probably consider making the move to rust.

Collapse
 
drumm profile image
Sam J.

Easy to choose, simply choose Rust instead.

Collapse
 
vanessatelles profile image
Vanessa Telles

In my opinion, it's a good idea to learn C first and then go for C++ or C# because it gives you a good base to learn other programming languages.

Collapse
 
yekyam profile image
Manuel Mateo

Could you explain point 6 more? I agree that because of higher-level constructs, C++ tends to be more secure, but I'm not sure how encapsulation could lead to more-secure code.

Collapse
 
swordheath profile image
Heather Parker

sure, here I meant that encapsulation as one of the C++ classes can protect straightforward access to the data, that's why this language is considered to be more secure comparing to C

Collapse
 
yekyam profile image
Manuel Mateo

How does encapsulation lead to more secure code?