Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert C to C++?

82 views
Skip to first unread message

Peng Yu

unread,
Dec 20, 2009, 3:11:59 AM12/20/09
to
I want to convert C programs to C++ programs. I found this tool
http://www.scriptol.com/scripts/ctocpp.php

But I'm wondering if this is the best tool to convert C programs to C+
+ programs.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Seungbeom Kim

unread,
Dec 20, 2009, 9:47:17 AM12/20/09
to
Peng Yu wrote:
> I want to convert C programs to C++ programs.

Most C programs are also C++ programs themselves, except that some
identifiers (such as 'class', 'delete') may conflict with keywords.
C++ does not require its programs to use certain style (such as
object-oriented) or features (such as classes).

What does it mean to you to convert C programs to C++ programs,
and why would you want to do it?

--
Seungbeom Kim

Jens Schmidt

unread,
Dec 20, 2009, 11:02:22 PM12/20/09
to
Peng Yu wrote:

> I want to convert C programs to C++ programs. I found this tool
> http://www.scriptol.com/scripts/ctocpp.php
>
> But I'm wondering if this is the best tool to convert C programs to C+
> + programs.

The best tool is a project team creating the C++ program from the
requirement specification. Especially a team knowing about OO design
and implementation.

Other tools probably result in a C program written in C++.

There is a famous saying:
"You can write FORTRAN in any language".
--
Greetings,
Jens Schmidt

Francis Glassborow

unread,
Dec 20, 2009, 11:00:34 PM12/20/09
to
Peng Yu wrote:
> I want to convert C programs to C++ programs. I found this tool
> http://www.scriptol.com/scripts/ctocpp.php
>
> But I'm wondering if this is the best tool to convert C programs to C+
> + programs.
>

Possibly once you are clear in your mind what you are trying to achieve.
Personally I would either leave the code alone (apart fgrom dealingf
with name/keyword clashes) or I would want to rework the code fgor myself.

Thomas Richter

unread,
Dec 20, 2009, 11:01:09 PM12/20/09
to
Peng Yu wrote:
> I want to convert C programs to C++ programs. I found this tool
> http://www.scriptol.com/scripts/ctocpp.php
>
> But I'm wondering if this is the best tool to convert C programs to C+
> + programs.

Well, there is typically not much to "convert", however, a good C
program, even with the minor adaptations to be a valid C++ program,
makes still not a good C++ program. For example, while "malloc" is
certainly a valid C and C++ function to allocate memory, and while it
certainly works alike in C and C++, a good(!) C++ program would rather
allocate objects via "new" and not "malloc". In C++, you would need an
additional cast there (from void * to the target type), but that's it.

Thus, what are you trying to do?

So long,
Thomas

Peng Yu

unread,
Dec 20, 2009, 11:00:32 PM12/20/09
to
On Dec 20, 8:47 am, Seungbeom Kim <musip...@bawi.org> wrote:
> Peng Yu wrote:
> > I want to convert C programs to C++ programs.
>
> Most C programs are also C++ programs themselves, except that some
> identifiers (such as 'class', 'delete') may conflict with keywords.
> C++ does not require its programs to use certain style (such as
> object-oriented) or features (such as classes).
>
> What does it mean to you to convert C programs to C++ programs,
> and why would you want to do it?

There a number of things that are different in C and C++ files,
including that the header files in C are not consistent when the
header files in C++.

I could use the instruction on the following page to compile C files
along with C++ files. But in one project, I feel that it might be
better to maintain only one languages (i.e., C++ files) rather than
two languages (i.e., both C and C++ files) if there is a way to
convert C files to C++ files.

http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html


--

Florian Weimer

unread,
Dec 20, 2009, 11:01:26 PM12/20/09
to
* Seungbeom Kim:

> Peng Yu wrote:
>> I want to convert C programs to C++ programs.
>
> Most C programs are also C++ programs themselves, except that some
> identifiers (such as 'class', 'delete') may conflict with keywords.
> C++ does not require its programs to use certain style (such as
> object-oriented) or features (such as classes).

Most C programs using malloc() are not C++ programs because C++ lacks
the implicit conversion from void * to any pointer-to-object type. It
is possible to address this using templates and the preprocessor
(without actual code changes), but I suppose there are other gotchas.

--

Ryan McCoskrie

unread,
Dec 20, 2009, 11:02:45 PM12/20/09
to
Peng Yu wrote:

> I want to convert C programs to C++ programs. I found this tool
> http://www.scriptol.com/scripts/ctocpp.php
>
> But I'm wondering if this is the best tool to convert C programs to C+
> + programs.
>

1) Adjust all of the standard headers by adding the character c to the
beginning and removing the .h at the end. For example:
#include <stdio.h> becomes #include <cstdio>
2) Check it compiles, adjust any miscellaneous problems.

--
Quote of the login:
It is easier to change the specification to fit the program than vice versa.

Frank Birbacher

unread,
Dec 23, 2009, 12:03:38 PM12/23/09
to
Hi!

Ryan McCoskrie schrieb:


> 1) Adjust all of the standard headers by adding the character c to the
> beginning and removing the .h at the end. For example:
> #include <stdio.h> becomes #include <cstdio>

A lot of functions are in the std:: namespace in C++. They are not in C
for C does not have namespaces.

> 2) Check it compiles, adjust any miscellaneous problems.

Despite sounding easy, it can take a lot of time.

Frank

--

Francis Glassborow

unread,
Dec 24, 2009, 3:59:00 AM12/24/09
to
Frank Birbacher wrote:
> Hi!
>
> Ryan McCoskrie schrieb:
>> 1) Adjust all of the standard headers by adding the character c to the
>> beginning and removing the .h at the end. For example:
>> #include <stdio.h> becomes #include <cstdio>
>
> A lot of functions are in the std:: namespace in C++. They are not in C
> for C does not have namespaces.

But they are accessible in the global namespace.


>
>> 2) Check it compiles, adjust any miscellaneous problems.
>
> Despite sounding easy, it can take a lot of time.
>

It depends how well written the C is. You may have problems if the
original has made use of the extras from c99 but well written C89 source
code compiles as C++ with very little change.

It is almost certainly better to leave good C code alone because turning
it into good idiomatic C++ is a major redesign problem. OTOH badly
written C is not worth reusing as C++ it needs a complete rewrite anyway.

#include <stdio.h>

is a perfectly valid C++ header, deliberately so. Companies such as
Adobe have ensured that their representatives on the INCITS C and C++
committees have ensured that it is possible to write source code that
compiles cleanly as both C and C++. They have spent time and money on
this because it matters to them.

Stavros K. Filippidis

unread,
Jan 4, 2010, 10:24:32 AM1/4/10
to
Is extern "C" {/* ... */} not suitable for your needs?

Stavros K. Filippidis
http://stavros.filippidis.name

On Dec 20 2009, 10:11 am, Peng Yu <pengyu...@gmail.com> wrote:
> I want to convert C programs to C++ programs.

> I found this toolhttp://www.scriptol.com/scripts/ctocpp.php


>
> But I'm wondering if this is the best tool to convert C programs to
> C++ programs.

{ Please see the FAQ about top-posting. Edits: excessively long quoted lines
manually formatted, quoted banner removed. Please don't quote the banner. -mod }

0 new messages