r/cpp • • Jun 08 '20

Programming in C vs C++

I’ve read about the differences between them on the Internet but I wanna hear more from a more personal perspective. Which one would you recommend starting with and to be proficient with?

0 Upvotes

43 comments sorted by

View all comments

Show parent comments

7

u/jmuffat Jun 08 '20

I guess what you’re really saying is C++ might not be the right choice of first language to learn, and I could agree with that. But I cannot agree that it is a good thing to learn char*, strlen, strcpy etc only to find out they’re terrible and, then, go misunderstand std::string because you think of them in terms of char*. Same for malloc/free, we all have been in the hell of tracking leaks, it’s not fun and it only teaches you one thing : that you need some way for the machine to manage this for you.

You’re probably someone who likes to know how things work before going to an abstraction, and that works : knowing assembly language was a big plus for me when learning C, I could see why things were one given way, what problems they were trying to solve. Yet, I wouldn’t recommend learning asm before learning C, it’s quite an easier trip the other way round... Do you see what I mean ?

6

u/jselbie Jun 08 '20

Thanks for commenting. And I mostly agree with you.

I think it's good know how strings work in C wrt to char* and null termination. Because that's that's what std::string is based on. Similar with raw pointers vs smart pointers (unique_ptr and shared_ptr).

On a long enough timeline, C++ developers have to integrate with legacy C APIs and libraries. If you are programming in C++ on Windows, you'll likely need to use the Win32 API at some point. On Linux, you'll need to make Posix library calls. These are both flat C APIs. Knowing pure C++ without the C foundation might make interop development a bit confusing.

-6

u/jmuffat Jun 08 '20

I'm afraid std::string's are not null terminated ;) (at least, they do not have to, don't rely on it)

I reckon you can treat the relationship to C API's the same way you treat them in Python : communicating with a different planet ;)

Plus, at least in Windows, UWP is much more C++ friendly than the old Win32 API.

But yes, at one point or another you are going to have to understand C, and you will have to have notions of assembly, because if you're going C++, it's quite probably that you're after either performance or interfacing at the hardware level (or both).

I guess it's a matter of whether you think bottom-up or top-down. So, probably that you want to do a bit of both :D

3

u/evaned Jun 08 '20

I'm afraid std::string's are not null terminated ;)

This statement as-stated is false, but it's actually pretty close to being right if you look at it from another perspective -- at least IMO.

The reason is that std::string can have embedded nulls. abc\0def is a totally valid std::string. Obviously you couldn't reliably pass that to a C API that expects a null-terminated string, but that's the C API's fault.

So while std::string strings do have a NUL at the end, they're not defined by it; and is such I don't think the quoted statement is entirely incorrect.