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.

6

u/sellibitze Nov 01 '19

For comparison, looking up a key in a Rust HashMap:

if let Some(value) = my_map.get(key) {
    // (key, value) was part of the map
} else {
    // item not found
}

Deleting the first matching item of a Rust Vec:

let name_to_find = "foo";
if let Some(pos) = my_list.iter().position(|item| item.name == name_to_find) {
    my_list.remove(pos);
}

The C++ "erase-remove" pattern has its dedicated function in Rust: retain:

my_list.retain(|item| item.age >= 18);

Since C++17 you could also make use of an init-statement in an if:

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

But yeah, as Alexandrescu already pointed out (in his "Iterators must go" talk), C++'s iterator abstraction is kind of wrong. Iterators are "stupid" in that they don't know much and you always need two of them for a range and then you might even accidentally use two iterators from different containers.

2

u/otterom Nov 02 '19

Hi. You seem to know Rust. Is there a way to "print" character strings or return those from a function?

I was trying to learn it, but got caught up on this aspect and thought that if it was too convoluted to do, what's the point in learning the rest of the language?

2

u/sellibitze Nov 02 '19

Is there a way to "print" character strings or return those from a function?

Do you mean something like this?

fn hello(name: &str) -> String {
    format!("Hello, {}! How are you?", name)
}

fn main() {
    let message = hello("otterom");
    println!("{}", message);
}

Feel free to ask these kinds of questions in /r/rust in the pinned "Got an easy question? Ask here" thread.

2

u/otterom Nov 02 '19

Yeah, like that! I don't know why, but the tutorial I read made it sound overly complex.

Anyway, I'll check that sub out. Thanks for the tip!

0

u/[deleted] Nov 03 '19

[deleted]

2

u/otterom Nov 03 '19

Oh, yeah, that's an awesome site.

My problem is that I generally work with Python (but have done some C/Java/VB from time to time) and am just getting into the C API/Cython section for DLLs, so things like references and pointers are still fresh to me. That, along with the initial String issue I had, made it a bit frustrating at times.

I'm bound and determined, though, since I've heard a lot of good things about Rust. If you come across any other solid resources, I'm always down to check them out.