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

8

u/jselbie Jun 08 '20 edited Jun 08 '20

While I agree with some points brought up by u/jmuffat (especially the unlearning part), I hold a minority opinion here.

I've been coding in C++ for over 20 years. I came up through the Basic -> Pascal -> C -> C++ path as well. And while I think C++ is far better for production code, C is a better language to learn from - especially for those new to programming. But you should quickly pivot to C++ as soon you get the foundations of C down.

It used to be that good C++ text books would just start off teaching C fundamentals, and then dive into C++ classes by the middle of the book. Modern C++ books jump head first with too many complex code examples that can confuse new programmers.

With C programming, you learn all the foundations first in addition to programming fundamentals. This includes the following:

  • Basic types: int, char, double, arrays, etc...

  • string fundamentals: char*, strlen, strcpy,etc...

  • Structure types: i.e. struct - the foundation of what a class is in C++)

  • Functions

  • dynamic allocation: malloc and free

  • Preprocessor

  • Stages for building programs: preprocessor, compiler, linker.

  • Pointers: And the relationship pointers have with arrays, this the core of C and C++, even if C++ is trying to abstract pointers away.

In learning C, you'll be quickly concluding that foundations that you've heard about in other languages are missing or weak. That's when it's time to move to C++. Everything you learned above is still 100% applicable in C++. With your knowledge of C, you can dive right into classes, inheritance, virtual methods, template and the standard C++ library.

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

6

u/jselbie Jun 08 '20

Can you show me an example of an std::string that isn't null terminated (or where its unsafe to assume as much)? Such that it wouldn't be safe to invoke .c_str() on it and pass the result to a C function expecting a const char * ?

-2

u/serviscope_minor Jun 08 '20

Can you show me an example of an std::string that isn't null terminated (or where its unsafe to assume as much)?

We have string views now; these are not null terminated.

7

u/STL MSVC STL Dev Jun 08 '20

std::string_view isn't std::string. Elephant seals are not elephants.

-5

u/serviscope_minor Jun 09 '20

std::string_view isn't std::string. Elephant seals are not elephants.

And how's that relevant?

std::string_views aren't std::strings but they are strings. The only reason we're having this conversation is because C++ basically has had this particular hole in its standard library for ages.

While yes, it's technically correct that the GP was talking about std::string, a large amount of the reason that people are talking about things in this way is because string_view is new. If you look at the context of the conversation, then what I'm saying is entirely reasonable. Here is the relevant piece of context:

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).

C strings are char* and null termination. C++ has 3 kinds of string by default now, C strings, std::strings and std::string_view. The middle one guarantees C-like semantics (null termination), but that's not what it's based on (size() is O(1)), and the latter is absolutely not. There's no reason to arbitrarily drop a chunk of the standard library just because it's new.

5

u/bikki420 Jun 09 '20

Everyone is explicitly talking about std::string. Re-read the posts above and don't be obtuse.

-1

u/serviscope_minor Jun 10 '20

Re-read the posts above and don't be obtuse.

So... obtuse is me actually reading the posts. Since you didn't bother let me repost the relevant quote for you:

string fundamentals: char*, strlen, strcpy,etc...

Then people start talking about std::string by comparison. C++ has another string type now: string_view. And since the actual conversation was about strings not std::string in particular, it seems really strange and counterproductive to ignore modern C++.

→ More replies (0)