1. Home
  2. Computing & Technology
  3. C / C++ / C#
photo of David Bolton
David's C / C++ / C# Blog

By David Bolton, About.com Guide to C / C++ / C#

A Tip : Compiling C with C++

Saturday October 18, 2008
If you develop in C, I suggest as a way of checking your code that you occasionally copy your files and rename the copied files extension to .cpp and compile them. Why? Because there are a number of places where C and C++ although with identical syntax behave differently and this can reveal bugs in your C code. C is a lot more lenient that C++ so enjoy the extra checking: odds are if C++ catches it then you probably didn't intend it!

For instance with enums, both C and C++ allow you to use the enum values as if they were int. This will compile in C, but in C++ it gives a cannot convert from main::trafficlights to main::color error.

int main(int argc, char * argv[])
{
    enum color {red,orange,yellow,green,blue,indigo,violet};
    enum trafficlights {tred,tyellow,tgreen};
    enum color g= red;
    enum trafficlights s = tred;
    g = tred;
    return 0;
}

C++ doesn't allow conversion from one enumeration type to another.

Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2009 About.com, a part of The New York Times Company.

All rights reserved.