2

I am learning C++, and I often encounter C++ code that has e.g. printf instead of std::cout, and int var[3] instead of std::array<int,3> var. I have never learned C, so these just look weird/ugly to me. For some reason, I don't know why, they just stand out as unnatural.

When I go ahead and Google them, they often show up as "C string, C array, C ...". I then move forward and Google: "Is C++ backwards compatible with C"; and I clearly find out that it is not. And people say "C and C++ are two different languages".

If I assume the 2 following statements are true:

  1. C++ and C are two different languages.
  2. Every individual source file is written in a single language.

Does this mean that things like the above, which show up as "C features" are actually C++ features that for some historical reason just look exactly the same as C? Would this be a correct way of looking at these (just refer to them as ugly C++, instead of C)?

Furthermore, is there always a "more C++ way" of doing the same as the "C features" that are part of C++? If there is, how can I tell my compiler to warn me about the "C features" so I can the Google the "more C++ way" and change them in my source file?

I use g++ version 11.1.0

Which doesn't complain about e.g. printf and int var[] which leads me to believe that they are indeed "ugly C++", but still C++.

Thanks

EDIT

I disagree with this question being a duplicate. I am not asking about the differences between C and C++ (as the "related" questions posted in the comments).

I am asking: Is the "C looking code" in a C++ file (which compiles with a C++ compiler) C or C++? Is the compiler helping me by mixing the C code with the C++? Or is it just really C++ only (which happens to look exactly like C).

PhysicsPDF
  • 361
  • 1
  • 9
  • 6
    *"C features" are actually C++ features that for some historical reason just look exactly the same as C"* This is correct. C++ started off as an extension of C, but the languages have been diverging for decades. So a lot of low level features in C++ are direct decendents from C, but often accumulate different limitations or requirements as C++ evolves. We usually refer to these as C features (like C string or C array) to indicate the version of the feature that comes from C as opposed to the C++ version like `std::string` or `std::array`. – François Andrieux Jun 30 '21 at 22:07
  • [Where is C not a subset of C++?](https://stackoverflow.com/q/1201593/327083) – J... Jun 30 '21 at 22:08
  • 1
    [Is C/C++ one language or two languages?](https://stackoverflow.com/q/14330370/327083) – J... Jun 30 '21 at 22:09
  • 2
    [Why is C++ backward compatible with C ? Why isn't there some “pure” C++ language?](https://stackoverflow.com/q/4575192/327083) – J... Jun 30 '21 at 22:10
  • 1
    There isn't always a "more C++ way" of doing *everything*. Just, there is *usually* a more C++ way of doing things that can be dangerous or difficult if you had to do it the C way. – François Andrieux Jun 30 '21 at 22:10
  • C++ was heavily based on C at first, but afterwards the creators realized that many basic functions needed a revamp but they still had to maintain backwards compatibility with the first C++ versions – Yash Jun 30 '21 at 22:10
  • 1
    @Yash Backwards compatibility with C isn't really a concern with regards to how modern C++ is evolving. C++ provides trivial types and `extern "C"` for when you need C compatibility, but today most C code is not legal C++ code. – François Andrieux Jun 30 '21 at 22:11
  • [Relationship between C and C++](https://stackoverflow.com/q/4388606/327083) – J... Jun 30 '21 at 22:11
  • [Interoperability of types in C and C++](https://stackoverflow.com/q/21421720/327083) – J... Jun 30 '21 at 22:12
  • [What are the fundamental differences between C and C++?](https://softwareengineering.stackexchange.com/q/16390/73708) – J... Jun 30 '21 at 22:12
  • [How is C different from C++?](https://softwareengineering.stackexchange.com/q/38942/73708) – J... Jun 30 '21 at 22:13
  • @J... the question asks why the functions are there not what the functions are – Yash Jun 30 '21 at 22:14
  • 1
    [Compatibility of C and C++](https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B) – J... Jun 30 '21 at 22:14
  • 3
    *"If there is, how can I tell my compiler to warn me about the "C features""* You can try to turn on more compiler warning. But C features are not always an error. Some times a raw pointer (`T*`) is the correct tool in C++, some times it is not correct. A compiler can't necessarily always help you write better code. – François Andrieux Jun 30 '21 at 22:14
  • 1
    @J... Yes, there are a lot of questions asking about the difference or similarity between C and C++. But linking *all* of them in the comments is basically spam. – François Andrieux Jun 30 '21 at 22:15
  • 1
    @Yash The question is expressing confusion about the relationship between C and C++. All of those links contain remedies to that confusion. – J... Jun 30 '21 at 22:15
  • I disagree with this question being a duplicate. I understand that the differences between C and C++ have been asked before. I understand they are different. I am asking if the "exactly looking C" code in a C++ source file is C or C++. – PhysicsPDF Jun 30 '21 at 22:16
  • 1
    @FrançoisAndrieux The point is that this is not an original question and that every possible angle of this question is covered in other answers on the stack. – J... Jun 30 '21 at 22:16
  • 3
    @PhysicsPDF If it compiles in a C compiler, it is C. If it compiles in a C++ compiler, it is C++. Sometimes the code is both. Sometimes not. – J... Jun 30 '21 at 22:17
  • 2
    @J... I would recommend finding the question that covers the same angle as this question, and linking just that question instead of a shotgun approach. – François Andrieux Jun 30 '21 at 22:17
  • 1
    @FrançoisAndrieux The point is that OP did not really try to find an answer themselves. This is not a question that we need to add yet another answer to. – J... Jun 30 '21 at 22:18
  • @J... Well, if it is a C compiler, it wouldn't compile because of the C++ features. If it is a C++ compiler, I could understand it being C++ then. I don't understand how it can be both C and C++ – PhysicsPDF Jun 30 '21 at 22:18
  • 2
    @J... *" If it compiles in a C++ compiler, it is C++."* That is not correct. A lot of code that is ill formed will compile. You can put all sorts of C source files in a C++ compiler which might compile without errors, but which wouldn't work as expected due to it having UB in C++. Because that code is C and not C++, despite the C++ compiler accepting it. – François Andrieux Jun 30 '21 at 22:19
  • 1
    @FrançoisAndrieux Nevertheless, it is valid C++ if it compiles - otherwise it wouldn't. A C++ compiler will only compile C++ code. That doesn't mean that the functionality of the code is expected to be the same when compiled in either compiler, it just means that it is syntactically valid. – J... Jun 30 '21 at 22:21
  • 1
    @J... The intent defines whether code is C++ or not. The definition of what constitutes C++ code can't be whether or not compilers accept it, because compilers can disagree on what code they will or won't compile. – François Andrieux Jun 30 '21 at 22:39
  • @FrançoisAndrieux Compiler defects are another matter entirely. The standard defines what the compiler *should* compile. – J... Jun 30 '21 at 22:47
  • `auto var = 3.5;` compiles and runs and is well defined in both languages, though does different things in each. – Mooing Duck Jun 30 '21 at 22:47
  • @J...: You're overlooking compiler defined behavior, which is only partially specified in the specification, and many different interpretations are valid. Example: is `char` signed? – Mooing Duck Jun 30 '21 at 22:48
  • 1
    @J... My argument is not about defects. Compilers offer different extensions, and different diagnostics. So whether a compiler rejects or accepts a code does not define whether it is C++ or not. Another compiler can disagree. As far as the standard goes, it does define that certain code must be accepted or rejected, but that doesn't cover everything. If you only include code that MUST be accepted as C++, then most C++ wouldn't fall under that definition. – François Andrieux Jun 30 '21 at 23:03
  • 1
    @PhysicsPDF Regarding your edit, if you compile the source file as C++, then it will be compiled according to C++ rules, including any feature that may also be present in C. `printf` is a C feature, but it is also a (mostly defunct) C++ feature. – François Andrieux Jun 30 '21 at 23:14
  • @FrançoisAndrieux Now *that*, I agree with. – J... Jun 30 '21 at 23:35
  • 1
    C++ was originally based on a (now) ancient version of C. Modern C has diverged from that ancient version. C++ has also evolved from its earliest incarnations. Modern C and modern C++ are siblings, with a common ancestor. – Eljay Jun 30 '21 at 23:42
  • _"...I am asking: Is the "C looking code" in a C++ file (which compiles with a C++ compiler) C or C++?..."_ C++ compiled to the relevant C++ language Standard. – Richard Critten Jun 30 '21 at 23:44
  • 2
    @PhysicsPDF *int var[3] instead of std::array var* -- Note that `std::array` didn't exist in standard C++ until C++ 11. If you're looking at C++ code written using the C++98 or C++03 standard, then of course you will see arrays declared the way that C code declares arrays. – PaulMcKenzie Jul 01 '21 at 00:02
  • if it's compiled by a C++ compiler in C++ mode then it's C++. End of the story – phuclv Jul 01 '21 at 00:41

0 Answers0