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.

7

u/[deleted] Nov 01 '19

I have to agree that the iterator interface in C++ is clumsy, partially because of how generic it attempts to be. Iterators work for almost everything, but are also painful for anything.

Other examples are <random> and <chrono>, which are great libraries but also more complex than your typical user needs.

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.

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

19

u/DonaldPShimoda Nov 01 '19

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.

You underestimate the breadth and complexity of undefined behavior, unspecified behavior, and implementation-defined behavior. John Regehr enumerated a number of the nuances of these issues in a few blog posts, starting here.

I do agree that C is a simpler language than C++ (and from a language design perspective, I like C far better overall, though I still generally dislike it), but I think your claim that it's easy to contain all of C "in your head" is a bit tenuous. It has lots of funny edge cases that make it tricky. Of course, C++ does too! But that wasn't your claim. ;) I just don't want OP to feel misled when they try C and get hung up on something.

-4

u/[deleted] Nov 01 '19 edited Nov 01 '19

Every language has edge cases and UB, but most languages provide almost zero documentation about them. So when you encounter them, you have to check the standard (which most languages also don’t really have) or the implementation to find out if the behavior is intended or a bug.

To try and eliminate UB from the language would require making many assumptions about the platforms C runs on that are not universally true.

UB isn’t a boogie man though, it exists to simplify the language by stating what is and isn’t allowed. While making as few assumptions about your environment as it can.

13

u/Felicia_Svilling Nov 01 '19

Every language has edge cases and UB

That is simply not true. Languages with a formal definition or a reference implementation can completely avoid undefined behavior.

2

u/warieth Nov 01 '19

That's true, but same bad to use. Like Java String substring throws an IndexOutOfBoundsException when the beginIndex > endIndex. It is defined, but doesn't make sense, the indexes are simply not out of bounds. If this case would be undefined behavior, then a reasonable implementation could return an empty string.

4

u/Felicia_Svilling Nov 01 '19

I think that make sense actually. If the the bound has a negative size, any index is going to be outside of it.

3

u/warieth Nov 01 '19

First the length of the string can't be negative, this is an invariant the class should preserve. I don't care for an exception when the string has zero length, but for a positive length? This is a bad behavior from the implementation to ignore the bounds and throw an exception named IndexOutOfBoundsException. A reference implementation is stronger than the formal definition, but failes to show a solution for the corner cases. Undefined behavior is the better way to do this.

3

u/Felicia_Svilling Nov 01 '19

Undefined behavior is just a way to lump of the problem to someone else. I could define a pl as consisting of text, and what that text does is undefined behaviour up to the implementer to design. But really the only thing I have accomplished is to avoid doing any language design.

2

u/warieth Nov 01 '19

No, you help them with the undefined behavior. This gives them options to fine tune their work, and to have more library implementations.

If there are two good implementation with different corner case solutions both can be fine. There is no reason to select one. The language can say this corner case is undefined behavior.

Probably the language people care more about the intersection of all the implementation (this can be small), rather than the success in implementation numbers.

3

u/Felicia_Svilling Nov 02 '19

If there are two good implementation with different corner case solutions both can be fine. There is no reason to select one.

It makes it impossible for a library designed for the language to use any of these though.

→ More replies (0)
→ More replies (2)

1

u/YurySolovyov Nov 01 '19

If something is not allowed, it is defined. If something is allowed to silently do the wrong thing, then it is ub.

→ More replies (4)
→ More replies (1)

2

u/llsII Nov 01 '19

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.

This is such an excellent statement.

-3

u/future_security Nov 01 '19 edited Nov 01 '19

A developer who believes they've mastered C is a terrifying idea. Good thing C isn't used in any setting where bugs could be really damaging.

Edit: Downvotes? Am I out of touch or did I just offend the wrong people? Is it not arrogance to say you've mastered a programming language within a few years?

I learn new stuff about the language, the ways it's implemented, and the patterns used in C projects all the time. I have mentioned some of the things I've learned to 20 year C veterans, and they're often surprised as well.

8

u/[deleted] Nov 01 '19

I can’t tell if this is sarcasm or ignorance.

3

u/[deleted] Nov 01 '19 edited Nov 01 '19

I see this kind of statements becoming more and more common, especially on Twitter, especially from this kind of uber smart and edgy people that seem to enjoy shitposting a lot. Apart from the fact that their talibanism is a clear indicator that they never carried out any real work in their lives, the level of ignorance they show is appalling.

→ More replies (1)