0

I have read that most of the new C++ compilers can compile both C and C++ programs and also read that not to use the extension .cpp to a C programming file so as to inform the compiler to compile in C. What is the difference in "C compile" and "C++ compile" using a C++ compiler. Is there any problem when a C program is given extension .CPP and compiled with a C++ compiler ?

Given a C program with C++ constrains( like not using typename, new, private, class etc as Identifiers ) how differently does a C++ compiler compile the C program with .CPP extension compared to a C compiler.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Trilok M
  • 681
  • 7
  • 21
  • 2
    This is the kind of thing that really depends on the specific compiler toolchain, IDE, build framework etc. Neither C nor C++ care about file extensions. But not all C is valid C++, so compiling C code as C++ can have mixed results. – juanchopanza Feb 17 '15 at 08:04
  • 2
    Yes, the program **might** fail to compile. C99 is no longer a subset of C++ for example. A C89 program should compile but `void*` pointers might need to be cast explicitly. Besides, what could you possibly gain? – sashoalm Feb 17 '15 at 08:04
  • 3
    The only problem could be is that C and C++ are DIFFERENT LANGUAGES!!! – Igor Pejic Feb 17 '15 at 08:05
  • 2
    Interesting question, but i'm not sure "what C code won't compile as C++" is appropriate for this site – tenfour Feb 17 '15 at 08:07
  • @tenfour It's not, it should be closed. – sashoalm Feb 17 '15 at 08:07
  • to use a C++ compiler to compile code that is largely C can be a good idea, if you are planning to gradually migrate to C++ anyway. If your code needs to compile on a C compiler anyway, stick to C, there are a lot of stuff they handle differently. – sp2danny Feb 17 '15 at 08:11
  • 1
    @tenfour not all C code can be compiled in C++. example typename is a valid identifier in C but not in C++. – Trilok M Feb 17 '15 at 08:12

3 Answers3

6

What is the difference in "C compile" and "C++ compile" using a C++ compiler.

Each interprets the source code according to the rules of a different language. One interprets it as C, the other as C++.

Is there any problem when a C program is given extension .CPP and compiled with a C++ compiler ?

Yes. Although C is very similar to a subset of C++, there are plenty of cases where valid C is not valid C++. For example, some keywords are reserved in C++ but not C:

int private = 42;
int class = 7;

and there are some type conversions allowed in C but not C++

char * array = malloc(32);

Any of these, and more besides, will cause a C++ compiler to fail if you lie about which language it's compiling.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
4

C++ compilers compiling C code is not a new feature, but a design strategy of C++ from the onset. However, a lot of perfectly valid C constructs don't compile as valid C++. Some of them are inoccuous, such as not being able to use names like new for variables, but some, such as mandatory casts of malloc result, require making valid C code less maintainable. Those changes make sense in C++ which offers "better" options to do the same, but not in C. Also, with the later evolution of C, some C99 features are actually not present and will never be present in C++. Thus using a C++ compiler may require you to modify perfectly valid C code, which is a non-trivial undertaking if it's written by others and can introduce defects.

Because of this it is much better to leave it to the C compiler - or, more precisely, the C frontend of the compiler suite you are using - to compile C code.

Community
  • 1
  • 1
user4815162342
  • 141,790
  • 18
  • 296
  • 355
2

C and C++ are really different languages and they have really different compilers. But c compiler is compatible with c++ compiler, so yes, you can build c program with c++ compiler. But the good way to do it is use extern "C" like this (definition __cplusplus is in gcc and compatible compilers)

YourFile.cpp

#ifdef __cplusplus
extern "C" {
#endif

//Code of your program

#ifdef __cplusplus
}
#endif
alexcleac
  • 29
  • 4