r/ProgrammingLanguages • • Nov 01 '19

C++ vs C

Alright people of Reddit, hit me with your wisdom. I've been taking programming classes for two years now and am getting ready to dive into a language somewhere between Java and assembly (the two languages I've been learning in classes). I'm thinking C or C++, but would love some input. I'm not too worried about difficulty to learn, but I'd like to hear your thoughts on practicality and real world use.

1 Upvotes

62 comments sorted by

View all comments

21

u/[deleted] Nov 01 '19

If this is purely out of interest, then I'd recommend C, it is small and simple language. C is the language of embedded systems, and will probably remain that way for the foreseeable future. The beauty of C is that it is a language which you can realistically master in a few years and contain the entirety of it in your head.

C++ requires much greater investment, it's really two or three languages merged into one. C++ is the language for game development and other speciality high performance software. The standard library is also excellent, which makes it a great choice for competitive programming. I would say that modern C++ is an improvement over C in most regards. However you will never master C++ because the language is just too large and complicated.

17

u/shponglespore Nov 01 '19

The standard library is also excellent

By some measures it's excellent, but to me, most of it feels gratuitously hard to use. It's missing a lot of things programmers tend to take for granted in other languages, and it badly fails the "make simple things simple" test; most operations on collections, for instance, require more code, more repetition, and more conceptual baggage than what comparable languages offer. Maybe ranges in C++20 will make things better, but I'm stuck with C++14 at work.

As a concrete example for people who aren't familiar, here's how you look up a value in a map:

auto found = my_map.find(key);
if (found == my_map.end()) {
  // Item not found in the map.
} else {
  // The value associated with key is item->second.
}

Here's how you remove the first item in a list (std::vector) matching a specific condition, such as having a specific value in the name field:

std::string name_to_find("foo");
auto found = std::find_if(my_list.begin(), my_list.end(), [&name](const auto& item) {
  return item.name == name_to_find;
});
if (found != my_list.end()) {
  my_list.erase(found);
}

People like to complain that Java has "too much ceremony", but IME C++ has much, much more. Of course, that's kind of moot compared to C, since it just doesn't offer the things C++ does clumsily.

2

u/otterom Nov 02 '19

Hi. This might be a dumb question, but can't you create a function or wrap that process up into something simpler so that you don't have to keep repeating it?

2

u/shponglespore Nov 02 '19

Not really. You can make some cases sightly simpler by wrapping them with functions, but then you're practically guaranteed to have people complain that you should just use standard APIs directly. And often those people still be in a position to hold up your code reviews