promotion image of download ymail app

Are all C programs C++ programs?

Do C and C++ have "a square is a rectangle but a rectangle is not a square" type of relationship? In other words, it makes sense to think that C++ cannot be compiled as a C program since it adds additional functionality but a C program can be compiled as C++ code. Is this actually true though?

Any input would be greatly appreciated!

Thanks in advance!

6 Answers

  • 10 years ago
    Favorite Answer

    That depends on what you define as a "C program" and what you define as a "C++ program".

    All C code will compile and run with C++ compilers, because C code is a subset of C++ code, but some purists would say that even though the code compiles and runs, it doesn't constitute a C++ program because it's not using, for instance, C++ input and output. C uses functions such as scanf(), gets() and printf(), while C++ uses stream classes and the << and >> operators.

    But I'm more liberal and would answer your question with a "Yes".

    Hope that helps.

    • Commenter avatarLogin to reply the answers
  • doug
    Lv 5
    10 years ago

    For the most part C++ includes full support for C. It was designed this way intentionally.

    from wiki:

    C++ is often considered to be a superset of C, but this is not strictly true.[21] Most C code can easily be made to compile correctly in C++, but there are a few differences that cause some valid C code to be invalid in C++, or to behave differently in C++.

    the C++ standard states:

    In addition to the facilities provided by C, C++ provides additional data

    types, classes, templates, exceptions, namespaces, inline functions, operator overloading, function name overloading, references, free store management operators, and additional library facilities.

    So basically, this states that C++ is C plus a bunch of other stuff.

    A C++ compiler, with a few exceptions, can handle straight C. A strictly C compiler would not be able to deal with C++.

    • Commenter avatarLogin to reply the answers
  • 10 years ago

    Not anymore, at one time C++ was just an extension of C. Since then, C has changed, and so has C++.

    However that being said it's not unusual for the same program to compile both C and C++. The method is the same, but there are just some differences int he libraries that you need the compiler to link to.

    • Commenter avatarLogin to reply the answers
  • Cubbi
    Lv 7
    10 years ago

    No, this is not true.

    It is only true that many simple C programs can be compiled by a C++ compiler and will execute in the same manner.

    But many other C programs will fail to compile by a C++ compiler.

    And some C programs will be compiled by a C++ compiler, but the results of their execution will be totally different, for example if they use sizeof('a') in a calculation.

    C++ and C share a common subset.

    • Commenter avatarLogin to reply the answers
  • How do you think about the answers? You can sign in to vote the answer.
  • 10 years ago

    I guess your trying to say is C++ a sequel to C?

    And can C++ language be translated to C and vice versa.

    First you have to know that the creator of C did not make C++.

    They are two different languages. Just very similar

    C++ was inspired by C.

    You probably already know this. But there are also other C type variates. Example C# C-- Objective-C.

    Did you know C was inspired by B?

    To answer the second part.

    C++ was created because C and other procedure-oriented languages could not address the complex problems that are present in real life. So the question of translating C++ code to C is absurd. You have to rewrite the code completely and that would be no small feat.

    So no you cannot traslate C++ to C.

    • Commenter avatarLogin to reply the answers
  • 10 years ago

    Thats exactly correct, C was created before C++. C++ was basically the same language with more features and tools. Though a C compiler won't recognize all of the new code that C++ offers.

    Source(s): long time programmer
    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.