7

Possible Duplicate:
“C subset of C++” -> Where not ? examples?

I am aware that C is a subset of C++ (i.e. there does not exist valid C code that is not valid C++ code). My question is whether g++ is completely compatible with all C code. For example, will

g++ -o testing test.c

produce an identical binary to

gcc -o testing test.c

in all circumstances?

More specifically, if they do not always create identical binaries, is there any reason that that could be a problem? Is it safe to always use g++ if I'm not sure about the code?

Community
  • 1
  • 1
ewok
  • 20,148
  • 51
  • 149
  • 254
  • 7
    `there does not exist valid C code that is not valid C++ code` - That's wrong. Just look at VLAs. – chris Jul 26 '12 at 18:25
  • 1
    C++ does have keywords that C code might use as variable names. – jxh Jul 26 '12 at 18:27
  • 2
    See: http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B – Doug T. Jul 26 '12 at 18:27
  • Yeah, C may be a "loose" subset, but it's not a strict subset. –  Jul 26 '12 at 18:27
  • In short, use a C compiler to compile C, a C++ compiler to compile C++. – Daniel Fischer Jul 26 '12 at 18:28
  • http://www2.research.att.com/~bs/bs_faq.html#C-is-subset –  Jul 26 '12 at 18:29
  • 4
    Where's my "close: question is predicated on a faulty premise"? – Stephen Canon Jul 26 '12 at 18:37
  • 1
    @StephenCanon I'm pretty sure that's the opposite of a reason to close a question. Point out the false premise and give examples of why it's false. exactly as has been done in the accepted answer. – ewok Jul 26 '12 at 18:42
  • @ewok: Not necessarily; "Not a real question" would apply too. –  Jul 26 '12 at 18:43
  • @0A0D Maybe sometimes, though I can't think of an example in which the false premise is causal of, rather than simply correlated to, the lack of question. Regardless, in this case, I think I have asked a clear question, which has been answered by someone correcting my false premise. – ewok Jul 26 '12 at 18:54
  • @ewok: It's not so much a false premise but an amalgamation of varied and different concepts that make it sound like not a real question. There is a hint of false premise but you jump from C being a subset of C++ (false) to how binaries are compiled across different compilers/languages. So its slightly rambling/not a real question. –  Jul 26 '12 at 18:59
  • 2
    @ewok: Also note that if your C headers are not prepared to be included from C++ you will also have linker errors (the C++ compiler will mangle the names, a C library would not, the symbols won't match). This is beyond whether all C code can *compile* in a C++ compiler. – David Rodríguez - dribeas Jul 26 '12 at 19:05

3 Answers3

10

C is not a subset of C++.

Try:

foo.c

int main() {
    int class = 0;
    return 0;
}

Anyway have fun here: Where is C not a subset of C++?

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
3

It's hard to figure out how to answer this:

  • C is not a complete subset of C++. There are several things in C that are not valid C++. Variable Length Arrays are one such thing. Implicit casts from void* is another.
  • What code g++ will accept depends on the flags passed to it. Is it compiled just by invoking g++ (which version?) Or with -ansi? -pedantic? How about std=<lang>?
  • and finally, whether the code is accepted is a completely different issue from "whether it produces an identical binary". Code which is accepted by both compilers might result in binaries which do the same thing, but which are nonetheless not identical.

Given all this ambiguity, it's impossible to give you a definitive answer.

jalf
  • 243,077
  • 51
  • 345
  • 550
2

C is not a subset of C++. It has never been. C99 is significantly different from C++, but even the classic C89/90 is not a subset of C++ with a large number of significant differences.

Even for C89/90 the differences that "break" valid C code under C++ compiler will include

  1. More restrictive implicit pointer type conversions in C++
  2. Nested struct declarations are class-scoped in C++, file-scoped in C
  3. Operator grammar (and the resulting operator precedence) is slightly different between the languages
  4. Tentative definitions are illegal in C++
  5. New keywords, no implicit int rule in C++...
  6. And so forth and so on
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    It is false to say that invoking `g++` on a `*.c` file will automatically invoke the C compiler. I just tested this myself and [this is the output](http://pastebin.com/dxT1EL4A) (note the invocation of `cc1plus`, the C++ backend compiler). File extension is used when you call `gcc`. The difference is that compiling a C source file with a C++ compiler will by default generate exception handling info and type information that would otherwise be missing. There are options for the `gcc -x c` C compiler like `-fexceptions` which force this information for C compilation. – rubenvb Jul 26 '12 at 18:57
  • @rubenvb: I stand corrected. Thanks. I thought that behavior extends to `g++` as well. – AnT stands with Russia Jul 26 '12 at 19:00