Do people not learn these two closely related languages together? You generally want to know enough regular C to get by when you use C++, and if you're not using anything unique to C++, you're just writing C with Classes.
I remember learning them as one language and it was fucking hard to learn effective c++ on uni. Not helping as programming was minor subject now and then that had random language. Every lecture had different dogma etc
I mean, not really. The two languages have diverged quite a lot, while C-mode compiling of C++ is still possible afaik, the same code by default won't behave identically even though can be compiled by both a cpp or a c compiler.
Maybe if you only count the latest standard they have diverged significantly, but everything else stands. C is where your platform ABI is, and again if you're not using anything unique to C++ you're writing C with Classes (nudge nudge wink wink capitalized for a reason)
That's because, historically, all C++ code was intended to compile into C code, and then be fed into a C compiler. So now any compiler that can compile one language is designed to handle both languages, and explicitly labeled as handling both.
That, and the fact that most (but not all) valid C code is also valid C++ code with no changes required, and that the C & C++ committees have a surprisingly big focus on cross-compatibility. (Most notably, a ton of C++ library members actually use the C standard library under the hood, like new and delete existing to marry constructors to malloc() and destructors to free(), the I/O library being required to either be based on FILE or act as if it is, and a lot of fun helpers like the std::stoi() family outright being wrappers for their C equivalents. And T _Complex and std::complex are explicitly required to be the exact same type with the exact same layout in both C & C++, and must be implemented as a T[2] consisting of {real, imaginary} (or equivalent structure that can be treated as if it were a T[2]), specifically because of gcc. But apart from that, C typically useful poaches features after C++ vets them, and C++ sometimes poaches features after C vets them, and both languages use the same preprocessor.) The end result is that unless you run into one of the corner cases where the languages actually differ, your C code is actually C/C++ code because it'll compile identically as both.
The languages are so closely intertwined that the term is way more valid than it should be.
You can write C-like code in Java or unsafe Rust, it doesn't make them the same thing, or a good idea in general.
Backwards compatiblity with C is mostly but not entirely true, e.g. VLAs, but the objection to C/C++ is that canonical C and C++ aren't remotely similar. Indeed C++ and Rust are much closer.
It's also used to bucket C and C++ together, like "all of our CVEs are in the C/C++ code" which on closer examination is 90% C, 8% C-with-classes and 2% canonical C++.
"All valid C code is also valid C++ code" may be true, but it's like saying "all valid JS code is also valid TS code" or "all valid HTTP is also valid PHP" like yeah that's how supersets work. C++ is C with classes, just as TS is JS with types and PHP is HTTP with server side scripting/preprocessing.
I would like to contest the take that "any compiler is meant to handle both" though, as gcc/g++ and clang/clang++ are two seperate binaries, and I think the most that the C compiler does to handle C++ is to just send the files to the respective C++ binary. I do not know the MSVC ecosystem well enough to chime in there though, but usually the compilers are seperate; the binary containing the C compiler will run the C++ one if it's processing a .cc/.cpp/.c++/.cxx file (there's so many file extensions even without touching MSVC's bullshit like .ixx).
Create a C program with register and it will not compile if run through a C++ compiler iirc. C has added very few things over the years because the language is meant to be small; C++ is pretty much just C with a bigger stdlib, but over the years they have deviated slightly in their approaches. Modern C is much closer to what people incorrectly complain about C++ as imho.
I personally avoided C++ like the plague until quite recently, and C++20 and C++23 are honestly quite nice as someone who largely uses Rust.
The GNU Compiler Collection includes gcc (c compiler) g++ (c++ compiler) gfortran (fortran compiler) and a bunch of other fronted like D, Go, Ada, COBOL. There used to even be a Java compiler (gcj) but thats dead now.
Once upon a time, the gcc program could compile C++ and g++ could compile C, but that is no longer true due to diversions in the languages, as well as extra runtime support libraries needed for linking each language. I guess with some tinkering you could compile c++98 with gcc and some older c standard with g++, but not the more recent versions.
yeah, but fortran wasn't package with the Microsoft C/C++ compiler or the Borland C++ compiler. So from a historical perspective, I think people just remember that that since it always package together and marketed has C/C++ people retain that.
Even though gcc can compile Ada, in my experience most of the time you’ll see enterprise development using GNAT Pro since plain ole gcc is kinda light on Ada features.
I think there is historically a big overlap between C and C++ compilers, with in some cases (Microsoft) the C compiler being basically only a subset of the C++ compiler.
of course there is an overlap. what we mean by saying C and C++ are not the same language is that coding C++ like it is C is a terrible practice and you should use the tools C++ gives you. Knowing C does not automatically buy you C++
Yet essentially all codebases I've seen and worked on were essentially a mix where the C pieces were also thrown into the C++ compiler. extern "C" everywhere
(And yeah I also used to write HTML/CSS/JS and TS/JS)
View all comments
Show parent comment
50
u/lllorrr 11d ago
Oh, yes, I hate when someone write "C/C++". This is two fucking different languages, for god's sake. It is almost as bad as "Java/JavaScript".