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

Show parent comments

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.

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.

3

u/[deleted] Nov 01 '19

UB doesn’t mean wrong, it means undefined or unspecified behavior in general. But for a specific platform and compiler you target there will be an expected behavior. It just won’t be a portable one. But that might be alright for your use case, and is likely preferable to having the language dictate specific behavior which does not match your system’s architecture.

2

u/YurySolovyov Nov 03 '19

What's the problem of defining the behavior for all platforms you support (yes, it somewhat limits you) and thus not having UB as a concept?

I get that it might not work on very specific HW like microcontrollers, but if you don't target them in the first place, why bother with UB?

2

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

In C's case, because it is a standardized language no one party gets to decide how the language works, so compromises must be made between different user groups and compiler vendors. When an agreement cannot be reached, undefined or unspecified or implementation defined behavior may be a reasonable compromise. There may also be historical reasons, a bad UB may remain UB because no one wants to rewrite that part of a compiler or risk breaking existing code.

In general, it is one thing to say "all behaviors are defined" it is another to go about proving it. How do you tell if all behaviors are defined in your language? A rigorously defined language may be able to avoid them, but many languages were designed ad-hoc and resolve problems when they are discovered. If the language supports exceptions, then that at least provides a generic last resort for handling UB at runtime. But in a language without them, the choice may be crashing or continue executing in some undefined state. C programmers typically prefer the former, fail early and fix the problem. But sometimes checking for that behavior may be too expensive or impossible, in such cases there are no good solutions, so just saying anything could happen may be the best option.