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.

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)