DEV Community

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

Posted on

What to choose: C vs C++?

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 systems, databases, etc. C is regarded as the "God" of programming languages, whereas C++ is an extended version of C. They have given so much to the programmers that it will be difficult to choose one over the other!

In today’s post, I'd like to compare two different languages and try to pick one (spoiler alert: this will be a tough task).

What is C?

C is a machine-independent structural or procedural oriented programming language that is widely used in a variety of applications. Dennis Ritchie, a great computer scientist, created the C language at Bell Laboratories.

C is a fundamental programming language that can be used to create everything from operating systems (such as Windows) to complex programs such as the Oracle database, Git, Python interpreter, and many more. Because it serves as the foundation for other programming languages, the C programming language has been dubbed "God's programming language." We can easily learn other programming languages if we know the C language.

What is C++?

C++ is a general-purpose, object-oriented programming language that was also known as "C with Classes." In 1979, Bjarne Stroustrup created this language. Because it supports both procedural and object-oriented programming languages, it is a multi-paradigm programming language. C++ has the properties of the C programming language, as well as classes and objects for user-defined data types. C++ is used in graphics applications, operating systems, smartwatches, game development, cloud-distributed systems, compilers, and other similar applications.

C++ is now used by top tech companies such as Google, Meta, Amazon, and many others. It is now not only an extension of the C programming language, but it has also become a popular and in-demand programming language due to its modern update and high performance.

In what aspects are these languages similar?

  1. Syntax;
  2. Code structure;
  3. Almost all of C's operators and keywords are present in C++ and perform the same function;
  4. Both models' basic memory models are very close to the hardware;
  5. Both languages have the same notions of stack, heap, file-scope, and static variables;
  6. They both have the same compilation;
  7. Most C operators and keywords are present in C++ as well.

What are the key differences between C and C++?

  1. Programming paradigms (C is a structural or procedural programming language; C++ is a structural as well as an object-oriented programming language);
  2. Subset (C++ is a superset of the C programming language; C++ can run 99% of C code, but C cannot run C++ code);
  3. Data types (C supports built-in data types; C++ supports both built-in and user-defined data types);
  4. Language type (C is a function-driven language; C++ is an object-driven language);
  5. Keywords (C contains 32 keywords; C++ supports 52 keywords);
  6. Security (C does not have any security features, so it can be manipulated by outsiders; C++ is a secure language as it offers security features such as data hiding and encapsulation);
  7. Headers (the C standard IO header is stdio.h; for C++, it is iostream.h);
  8. 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);
  9. Approach (for C, a top-down approach; for C++, a bottom-up approach);
  10. Reference variable (the C language doesn’t support RV; C++ supports RV);
  11. Inheritance (the C language doesn’t support inheritance; C++ supports inheritance);
  12. Overloading (the C language doesn’t support overloading; C++ supports overloading);
  13. Input and Output Function (In C, the scanf() and printf() functions are used to take the input and output, respectively; in C++, the cin and cout functions are used to take the input and output, respectively);
  14. Meta-programming (macros + _Generic() for C; templates (macros are still supported but discouraged) for C++).

Which approach is better? 

When using different programming languages such as C and C++, neither approach is superior. It all comes down to personal preference. Both can be used by skilled programmers to create a fully functional program. However, bottom-up is usually preferable for groups and top-down for individuals. Bottom-up tends to be messier than top-down, which is more organized by default. From that point of view, I believe that C is more preferable to learn.

One more argument to learn C is that C++ is based on this language, so all fundamental rules and principles are alike for both of these languages. One of the options is to study C in the first place and then learn C++. In that case, you will have the ability to dive into each language and, depending on the project you are working on, make your choice.

To be honest, I don’t think that it is a truly good question—what language to choose among C and C++. It is obvious that these two programming languages have some similarities, but in most cases they serve different purposes and are used in different projects. Which language to choose is totally up to the developer who is going to work with it.

For me, C++ is more preferable, due to the fact that it has more features and more applications, which allow me to explore various roles. Learning C++ is also easier, especially if you are familiar with object-oriented programming. Knowledge of object-oriented programming will take you a long way to mastering C++. Of course, this experience is not required. 

Which language do you prefer, C or C++? Share your experience in the comments!

Top comments (28)

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?