First: the question (and some other answers) seems to be based on the erroneous premise that C is a strict subset of C ++, which is actually not the case. Compiling C as C ++ is not the same as compiling it as C: it can change the meaning of your program!
C will basically compile as C ++ and will basically produce the same results, but there are some things that are explicitly defined to give different behavior.
Here is a simple example - if this is your ac :
#include <stdio.h> int main(void) { printf("%d\n", sizeof('x')); return 0; }
then compiling in the form of C will give one result:
$ gcc ac $ ./a.out 4
and compilation, since C ++ will give a different result (unless you use an unusual platform, where int and char are the same size):
$ g++ ac $ ./a.out 1
because the C specification defines a character literal of type int , and the C ++ specification defines its char type.
Secondly: gcc and g++ are not "the same compiler." The same end code is used, but the front ends of C and C ++ are different pieces of code ( gcc/c-*.c and gcc/cp/*.c in the gcc source).
Even if you stick to parts of the language that are defined for the same, there is no guarantee that the front of C ++ will parse the code in the same way as the front of C (i.e. the same input to the back end), and, therefore, does not guarantee that the generated code will be identical. Thus, it is possible that in some cases it is possible to generate faster code than another, although I would suggest that you need complex code in order to have at least some chance to find the difference, since most of the optimization and code generation occurs in general end of compiler; and the difference can be a round anyway.
Matthew slattery
source share