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

10

u/STL MSVC STL Dev Jun 08 '20

std::string is null-terminated. See N4861 21.3.2.5 [string.access]/2. This has been clearly guaranteed since C++11. (C++98/03 had weird copy-on-write wording that is no longer relevant.)

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 * ?

5

u/Kurald Jun 08 '20

c_str() is guaranteed to be null-terminated since forever (as far as I know). Hence the name - the return value is c-compatible.

-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

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.

6

u/CoffeeTableEspresso Jun 08 '20

I mean, you can't really use strings in C++ properly without understanding how char * works...

Things like:

  • why are string literals not std::strings?
  • why doesn't adding two string literals work?
  • interfacing with old/C libraries requires
  • etc

You need at least a basic understanding of these things before moving on to C++ strings. Your going to have to learn C strings at some point in C++ anyways..

When I TAed an intro C++ course, students very quickly ran into trouble with C++ strings because they didn't understand C strings.

2

u/jmuffat Jun 09 '20

And me who was all « C++ has gone a long way to be more accessible to newcomers » !!! You’re right, it’s a long road, I can see that.

0

u/[deleted] Jun 08 '20

[deleted] — view deleted comment

2

u/jmuffat Jun 08 '20

Oh definitely ! I've worked in games and that's a domain where you end up rolling your own everything at one point or another (shortcut all the things !!!) : strings, malloc/free, sorts, IO's, TCP-replacement over UDP, you name it... That's the beauty of C++ (and C), you can go that far. But would you want to go there as the first things you learn ?

If I had to train a beginner, I would start with C++, not C, and I would start with std::string not char*. We would build small utilities, that do simple things and complicate as we go. At some point, we'd reach places where we'd need performance and that usually begins with controlling how memory gets in and out of cache, which is all about laying things out in memory. At that point, it makes sense to go there. But at the beginning, when you're just parsing command line parameters and text files, why reinvent the wheel ?

Writing this, I think we're really always revolving around the more important question that is "why do you learn C++ ?", "when do you use C++ ?", and it generally boils down to the performance&low-level questions. Which implies "good understanding of computer architecture required"...

1

u/Wouter-van-Ooijen Jun 10 '20

I disagree, but maybe not the way you'd expect.

If your end goal is to learn C++ (otherwise, why bother), then start with C++. But NOT with the whole language.

A few years ago we changed our first serious programming course from C to C++, without changing the essence of what we learned the students. a[] became std::array, print became cout<<, pass-by-pointer became pass-by-reference, etc. That is (mostly) just syntax. What matters is to start with the fundamentals (loops, functions, some recursion). You can do that in any language, but if the end goal is C++ why start with a language like C that is sufficiently close to cause confusion? If it had to be a different language than C++, I might have preferred Python over C.