Writing C code in VS2005
I have visual studio 2005 and have been writing C code in it. However, I have done this in a rather round-about way. I created an empty c++ project, added a .cpp code file, wrote normal C into it and compiled, which works.
Is there a way to create a C project instead of me having to create a c++ one?
I do not think that there is a C project option, but of course you can give your source files a .c extension and change the project settings to have the code compiled as C code (/TC, under Configuration Properties -> C/C++ -> Advanced).
And if the file is already called something .c instead of .cpp, it will automatically choose to compile it as C rather than C++ (Unless you specifically change it with the /TP option of course).
Originally Posted by laserlight
If the files do not have the right naming convention, I suggest you rename the files rather than using the /TP or /TC option, since anyone else looking at your code would be terribly confused by C code written in a file called .cpp or C++ code in a .c file.
The third solution, if you really want, is just to let it compile the C code as C++ code.
Visual C++ doesn't support the C99 standard so pretty much everything in c is available under C++ too.
And well written C++ code (without using new/delete) is compatible with C, as well.
I wouldn't really go that far.
Originally Posted by Elysia
Assuming you limit yourself to the C half of C++ and not use classes or anything, there are still some very minor differences that would stop some C++ code from compiling in a C compiler.
I agree. Most of the differences may be benign, but code that compiles in C++ (without using C++ features) may not compile in a C compiler. And unless it is known that the code will always be used on a system that will have a C++ compiler, it is not a good idea to use a C++ compiler to compile C. There is a risk that some C++ specific features sneak in there that make the code incompatible with standard C.
Originally Posted by cpjust
If for no other reason than C++ requires casts on malloc() when C doesn't.
Yes, well, I wouldn't go as far as to recommend it. I'll be happy just to mention it's an option. It has its ups and downs, though.
But these are basically the 3 choices you have to compile your C code.