As I know that .c is the extension format for c coding while cpp is the extension for c++. But I noticed that there are some c++ project use .c extension instead of cpp, is it both extension is supported in c++ compiler?

.c files are generally passed to the C compiler.
Are you sure those files really contain C++ source code?

Many bigger projects use building (compilation) tools such as cmake which select compilers based on the source file extensions, and .c would be passed to the C compiler. Some projects often use .c files (C code) as third-party sources for some special purposes such as hardware drivers and other IOs, i.e. low-level libraries. Or often "scientific" coding involves algorithms that were programmed in C (or even in Fortran) and it's often more robust to mix C and C++ instead of porting the C code to C++, because that could introduce bugs (during the translation, e.g. typos) into these older and well-proven C algorithms.

About .c being supported by c++ compilers, that depends on the compiler, some ignore extensions others do not. But generally speaking, compiling C source files in C++ can sometimes work just fine, sometimes not at all, and sometimes it will just produce a lot of warnings like "use of old C-style cast" or "... will violate strict aliasing rules", etc.

But coding c++ code in a .c file is not a good idea, and really what would be the point of that? to make the file name two characters smaller?

Read this. For a more detailed explanation of mixing C and C++, the why and the how.

I have double checked, is .C (upper case) instead of .c (lower case).
That is a project developed for running in unix, and in this link:http://filext.com/file-extension/C, is said that certain unix compiler only support the extension .C for c++.

Just curious, anybody know that any unix compiler that only support .C but not .cpp?
Because the one I use support both.

Can I mix .C and .cpp in the project if the compiler support both?

Yes you can mix both C and CPP files in the same project. C functions can not generally call c++ functions, but c++ functins can call C functions. You just have to tell the compiler which c functions you want to call from the c++ functions.

This is how you might code a header file that can be included in both C and C++ files. This example shows how to declare a function foo() that is contained in a C file and called from a c++ file. The _cplusplus may be something else on your compiler so check your compiler's documentation to find out what it uses. One way of easily doing that is to look at stdio.h and find out how your compiler handles that macro.

#ifdef __cplusplus // if compiling c++ code
exern "C" {
#endif
extern int foo();
#ifdef __cplusplus // if compiling c++ code
}
#endif

Yes you can mix both C and CPP files in the same project. C functions can not generally call c++ functions, but c++ functins can call C functions. You just have to tell the compiler which c functions you want to call from the c++ functions.

This is how you might code a header file that can be included in both C and C++ files. This example shows how to declare a function foo() that is contained in a C file and called from a c++ file. The _cplusplus may be something else on your compiler so check your compiler's documentation to find out what it uses. One way of easily doing that is to look at stdio.h and find out how your compiler handles that macro.

#ifdef __cplusplus // if compiling c++ code
exern "C" {
#endif
extern int foo();
#ifdef __cplusplus // if compiling c++ code
}
#endif

hi Ancient Dragon,
As mentioned in the previous post, I mistakenly treat the .c same as .C file, so the question suppose to be "can we mix .C and .cpp" (instead of .c and .cpp), both are cplusplus source file, just some linux compiler support .C for cplus cplus.

For my case, i tried both, the compiler support.
For the question, "can we mix .C and .cpp", i tried, yes, it works!

BTW, thanks for helping~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.