C++ vs C compilers

Hello.  We are porting an old (but still very useful) BASIC program to C.  We want to keep things as simple as possible, we intend to make this port as direct as possible (not because we are lazy, but because this is safety-critical software and we are very afraid of breaking anything), we are not going to make it object oriented, so we are sticking to C and we will keep the structure as similar as possible.

So we are selecting a compiler to use for our new product.  The target is 64-bit Windows.  What we are wondering now is if using a C++ compiler to create a C program is an appropriate choice, or if we should stick to selecting from C compilers.  In other words, is there any disadvantage to

1) Using a C++ compiler (eg a Visual Studio C++ Console EXE) and simply *not* using any C++ features such as classes,
2) Using an actual C-only compiler (since we are essentially writing a C program)

Is there any disadvantage to the first one?  Anything that makes that an inappropriate choice?   Like I said we need the result to be as reliable as possible, to run as similarly to the original version as possible.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
I think you can use the Visual studio C++ compiler, the default mode it works is as C++ compiler, but there is a way to make it work as C compiler. You can change this by going into Project Properties > C/C++ > Advanced --> Compile As -->  (/TP or /TC). I think there must be a way to do these settings from command prompt/console as well.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:
Hi Ron,

C and C++ programs link with different libraries, have different main entry points, and share little or nothing at runtime.

Feel free to use whichever compiler you prefer.  If you compile and link a C program you'll get a similar runtime object regardless of which compiler you use.

Karrtik IyerSoftware ArchitectCommented:
Here is an example to do it from console.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Julian HansenCommented:
Is there any disadvantage to the first one?
No - but just to clarify C++ is a super-set of C - all the old C (++ the new stuff)

You can still use C++ code and not build an object oriented application, in fact your "safety" comment directly applies - why use standard C to code up a bunch of code that has already been written and tested in STL libraries etc. Seems to me a safer bet is using the C++ constructs for your code rather than limiting yourself to a much more restrictive development environment.
RonMexicoAuthor Commented:
@Karrtik Iyer: Thank you, I did see that, that it is supported in Visual C++ compiler; I am asking more about the pros/cons vs using a dedicated C compiler, if any.  

We don't want to overcomplicate things, on the other hand there seem to be more widely used (hence probably more mature and reliable) options for C++ compilers for Windows targets.

What do you think?
RonMexicoAuthor Commented:
@JulianHansen: I would probably agree if we were writing code from scratch.  But we are porting venerable code that has LOTs of miles on it and been very tested.  In fact all it does is read input files, perform calculations, and create output files.  We don't intend to bring in any new libraries if we can at all avoid it -- just port BASIC loops to C loops, etc; BASIC file access to standard C file access, etc.  Does that make sense?
Karrtik IyerSoftware ArchitectCommented:
If you use a C++ compiler for a C code (which already works on C compiler) then you might also get lot of compilation issues.
C++ compiler is more types strict.
One example is you will have to add casts where void* are mixed with pointers of other types.
Myclass *ptrmyclass;
ptrmyclass= malloc(sizeof(*ptrmyclass));

The above code shall compile in C compiler, but in C++ you will need below cast
Myclass *ptrmyclass;
ptrmyclass= (Myclass *)malloc(sizeof(*ptrmyclass));

Also there are new reserved words in C++.
E.g:  "class", "and", "bool", "catch", "delete", "explicit", "mutable", "namespace", "new", "operator", "or", "private", "protected", "friend", etc. Hence these key words cannot be variable names.

To know the complete list of incompatibilities, please refer link below.

Also name mangling is handled differently in C++.

Hope this helps.

Kent OlsenDBACommented:
Languages don't typically have an easy 1 for 1 conversion.  Converting basic to C may not be trivial.

  LET A$ = B$


  char *A;
  char *B = "Some Text";

  A = strdup (B);

Basic automatically handles integer and floating point in the same variable.  C typecasts on the fly so the order of operation could drastically affect seemingly simple calculations.

Don't make the mistake of thinking that this will be a trivial conversion.  There are a ton of little 'gotchas' that will have to be sorted out.  The conversion may be easy, but the coder needs to be well versed in Basic and C to know where the tricky spots lie.

Karrtik IyerSoftware ArchitectCommented:
Hi Ron,

I do not see any disadvantage as such in using C++ compiler. However certain strategies (as explained in my earlier post) used by a C++ compiler may be different than a C compiler. I think most of the C++ compilers would have same or more optimization for the same set of C features as compared to a pure C compiler.
So I would say go for a C++ compiler given that you have still not written any C code.

evilrixSenior Software Engineer (Avast)Commented:
It's really simple, just ensure you C source code files are named with a .c and not a .cpp extension and Visual Studio will just assume they are C not C++ and so compile and link them accordingly. You can use the compile options to force all source files to build as C code, but it's really not necessary. As long as your source files have the correct file extension Visual Studio will assume and treat them as C files.
evilrixSenior Software Engineer (Avast)Commented:
Incidentally, you'll actually probably find building this as C++ and making use of the C++ standard library, which has support for proper string types (see Kdo's point above) is actually better than trying to confirm this to C. You don't have to create your own objects in C++ but you can make use of those that are already part of the standard library to greatly simplify the conversion process. The C programming language is much lower-level language than C++ (although C++ is mostly a super-set of C) and you'll find you end up having to do a whole heap more coding just to re-implement what C++ already provides by default.
Karrtik IyerSoftware ArchitectCommented:
Also in future if you want to extend this C code or convert it to C++ or add C++ specific features, then your C code shall already be compatible with C++ standard and compiled with C++ compiler. So you should be able to save some effort.
RonMexicoAuthor Commented:
Thanks all!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.