8 ms·
A quick word of warning: be sure to treat C and C++ as two completely separate languages that just happen to have similar syntax. Yes, you can use C++ as "C wit
by tonysdg 8y ago
A quick word of warning: be sure to treat C and C++ as two completely separate languages that just happen to have similar syntax. Yes, you can use C++ as "C with classes" (I and many others sure have at times), but you're doing yourself a disservice most of the time if you do.
- Waterluvian 8y agoHuh. I always perceived C to be a subset of C++.
- rhn_mk1 8y agoCurrent versions of C have some features that are not present in current versions of C++ (e.g. designated initializers). But that's not the point. C and C++ are in practice used completely differently, so writing C++ using C concepts with some additions rarely happens in the wild.
- 95014_refugee 8y ago... except in the firmware space, where important C++ mechanisms (especially RTTI and exceptions) tend to be unavailable, giving rise to a (useful, powerful, fun) hybrid of the two.
- adrianN 8y agoThat used to be true, but becomes less true with each new version of the respective language specs. Sometimes the differences are obvious because legal identifiers in C can be keywords in C++, sometimes the difference is subtle, like with the rules regarding type punning. But the major point is that there are almost always safer or more ergonomic ways to do things using C++ features that are not present in C.
- SamReidHughes 8y agoFWIW I think he's wrong. Basically, when writing C, you should write code the same as if you'd write it in C++, except that you're using C. Because sometimes that's annoying, things can get less type safe, but in your head, there's a C++ program that you're representing.
- jstimpfle 8y agoPrograms written in this mindset might be the number one reason why C has a bad reputation. Of course, if you just emulate what other languages automate for you, you should better write in these languages. But in reality, why C is still the best programming language for large projects (IMO) is exactly that the programmer is allowed to choose a suitable structure, such that the program can fulfill the technical requirements. Other languages force the project into a structure that somehow never fits after a couple thousand LOC.
- SamReidHughes 8y agoWhat. What good programs are written in C that don't have well-structured memory management the way C++ does it with RAII? Edit: "But in reality, why C is still the best programming language for large projects (IMO) is exactly that the programmer is allowed to choose a suitable structure, such that the program can fulfill the technical requirements. Other languages force the project into a structure that somehow never fits after a couple thousand LOC." Yeah, this doesn't make any sense. The reason is, C++ doesn't impose anything on your program structure that C doesn't, while C, with the limitations it has, imposes a tax on all sorts of ways of structuring your program. For example, you can't practically write a program using a futures library (such as the Seastar framework) in C. And every program you write in sensibly written C can be translated to C++. The exception might be really really small-scale embedded stuff that doesn't allocate memory.
- pjc50 8y ago> What good programs are written in C that don't have well-structured memory management the way C++ does it with RAII? MISRA C standards, popular in embedded projects especially automotive, ban the use of memory management altogether. The whole point of RAII is that the compiler manages it for you as far as it can. This is impossible in C because you have to do it manually. You might end up writing malloc() at the top and free() at the bottom of functions but that's the opposite of RAII.
- deleted 8y ago
- barrkel 8y agoC enum values are convertible from int; C++ enum values aren't. This is one of the biggest differences in fairly idiomatic C code and has been the case for a very long time (i.e. not dependent on newer C features not being in C++). #include <stdio.h> typedef enum EFoo { FOO_A = 1, FOO_B = 2 } TFoo; int main() { TFoo foo = FOO_A | FOO_B; printf("foo = %d\n", foo); } This compiles in C but not C++.
- elteto 8y agoWhat? https://godbolt.org/z/vnwvch https://godbolt.org/z/vnwvch
- antaflos 8y agoThat's not the same code, is it? GP's code does not seem to compile in C++: https://godbolt.org/z/5oAIYE https://godbolt.org/z/5oAIYE But it does in C: https://godbolt.org/z/zdTKiE https://godbolt.org/z/zdTKiE
- adrianN 8y agoTry creating a foo https://godbolt.org/z/AlHDhg https://godbolt.org/z/AlHDhg
- signa11 8y agoimho, c :: c++ == ipv4 :: ipv6 (where '::' should be read as 'is to' relation)
- wheels 8y agoC++ is largely backwards compatible to C, but idiomatic code is very different in modern practice between the two. A somewhat dated blog post I wrote on the topic a decade ago: https://blog.directededge.com/2009/05/21/c-and-c-are-not-the-same-language/ https://blog.directededge.com/2009/05/21/c-and-c-are-not-the...
- gnulinux 8y agoI like to think what is JSON to Javascript is C to C++ (not capability wise). JSON and Javascript are completely, entirely different things even though JSON is technically a subset of Javascript (every valid JSON sentence is a valid Javascript sentence). C and C++ are extremely different languages -- mostly due to cultural reasons -- even though every valid C program is a valid C++ program (not strictly true, C and C++ diverged slightly, but you got the point). I, for example, write C-ish C++ and compile it with g++ but call it "C" because it's "nothing like C++" and "it's basically C with some extra keywords". I know this comment sounds absurd but C++, especially modern C++, is a very complicated beast with a complex ecosystem and culture attached to it. If you inherit a C program and add one C++ line and compile it with g++, even though it's "technically" C++ now, I really wouldn't call it C++. The way you solve problems in C and C++ are extremely different, and this matters.
- Waterluvian 8y agoCan I be pedantic? If I'm wrong someone will correct me and I'll learn something. Json is not exactly a subset because it supports representation of numbers of any precision. JavaScript will convert to an IEEE 754. But it's up to a json deserializer to decide how many decimals to use. I think.
- gnulinux 8y agoI mean sure, doesn't change my point (pedantically C isn't a subset of C++ either). That was just a metaphor.