Thread: Writing C code in VS2005

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    15

    Writing C code in VS2005

    Hi

    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?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    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).

    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    And well written C++ code (without using new/delete) is compatible with C, as well.
    I wouldn't really go that far.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    I wouldn't really go that far.
    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.

    If for no other reason than C++ requires casts on malloc() when C doesn't.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  2. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  3. professional programmers, do you spend more time writing or modifying code
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 11-25-2002, 10:54 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM