Having just started on pretty much the same thing a few months ago (on a ten-year-old commercial project, originally written with the "C++ is nothing but C with smart structs" philosophy), I would suggest using the same strategy you'd use to eat an elephant: take it one bite at a time. :-)

As much as possible, split it up into stages that can be done with minimal effects on other parts. Building a facade system, as Federico Ramponi suggested, is a good start -- once everything has a C++ facade and is communicating through it, you can change the internals of the modules with fair certainty that they can't affect anything outside them.

We already had a partial C++ interface system in place (due to previous smaller refactoring efforts), so this approach wasn't difficult in our case. Once we had everything communicating as C++ objects (which took a few weeks, working on a completely separate source-code branch and integrating all changes to the main branch as they were approved), it was very seldom that we couldn't compile a totally working version before we left for the day.

The change-over isn't complete yet -- we've paused twice for interim releases (we aim for a point-release every few weeks), but it's well on the way, and no customer has complained about any problems. Our QA people have only found one problem that I recall, too. :-)

Answer from Head Geek on Stack Overflow
🌐
CodeConvert AI
codeconvert.ai β€Ί c-to-c++-converter
Free C to C++ Converter β€” AI Code Translation | CodeConvert AI
This tool can convert a wide range of C code to C++, from simple functions and algorithms to complete programs with classes, error handling, and complex logic.
🌐
CodingFleet
codingfleet.com β€Ί code-converter β€Ί c
Convert Your Code to C - CodingFleet
C Code Converter - this online AI-powered tool can convert any code to C. Enjoy seamless conversions and unlock cross-platform development like never before.
Discussions

Converting C source to C++ - Stack Overflow
Convert huge classes into instances with appropriate constructors and initialized cross-references; replace static member accesses with indirect accesses as appropriate; and get that working. Now, approach the project as an ill-factored OO application, and write unit tests where dependencies are tractable, and decompose into separate classes where they are not; the goal here would be to move from one working program ... More on stackoverflow.com
🌐 stackoverflow.com
How to convert C++ Code to C - Stack Overflow
The front end of a compiler analyzes ... e.g. C++ is designed to help the human programmer to write code, the intermediary form is designed to help simplify the algorithm that analyzes said intermediary form easier). The back end of a compiler takes the intermediary form and then converts it to a 'target ... More on stackoverflow.com
🌐 stackoverflow.com
How to convert C to C++? - Stack Overflow
How can I convert C code to C++? When I try my best, I failed every time. Can anyone help me? In particular, I'm trying to understand the following: v=v?v%(5*r)*n--:v/10 I know if v == v mod... More on stackoverflow.com
🌐 stackoverflow.com
How to convert a C++ program into a C program?
I write a program in C++ environment but I want to directly convert that one into C program. Is there any technique to convert directly? More on researchgate.net
🌐 researchgate.net
8
0
August 27, 2012
People also ask

Is the C to C++ converter free?
Yes. You can convert C to C++ for free without creating an account for up to 5 conversions per day. For higher limits and additional features, you can sign up for a Pro account.
🌐
codeconvert.ai
codeconvert.ai β€Ί c-to-c++-converter
Free C to C++ Converter β€” AI Code Translation | CodeConvert AI
Is the C++ to C converter free?
Yes. You can convert C++ to C for free without creating an account for up to 2 conversions per day. For more conversions and higher limits, sign in for free β€” every account gets 5 credits per day with support for up to 25,000 characters per conversion.
🌐
codeconvert.ai
codeconvert.ai β€Ί c++-to-c-converter
Free C++ to C Converter β€” AI Code Translation | CodeConvert AI
Can I also convert C++ back to C?
Yes! CodeConvert AI supports bidirectional conversion. You can convert C++ to C just as easily by using our C++ to C converter.
🌐
codeconvert.ai
codeconvert.ai β€Ί c-to-c++-converter
Free C to C++ Converter β€” AI Code Translation | CodeConvert AI
🌐
Reddit
reddit.com β€Ί r/c_programming β€Ί how to convert c++ code to c
r/C_Programming on Reddit: How to convert C++ Code to C
June 4, 2019 -

I have some C++ code. In the code there are many classes defined, their member functions, constructors, destructors for those classes, few template classes and lots of C++ stuff. Now I need to convert the source to plain C code.

Will I have to do total rewrite of the code ?

Top answer
1 of 5
61

Pretty much, yes.

The classes become structs with a (separate) set of functions; usually you prefix the names so it’s clear what they act on. You’ll need to to manual de-/initialization too. E.g.,

class C {
private:
    int x;
public:
    C() : x(0) {}
    C(const C &c) : x(c.x) {}
    C(int xv) : x(xv) {}
    int getX() const {return x;}
};

becomes something like

typedef struct C C;
struct C {
    int x;
    /* If you need to, you can also partition this like
    struct {
        int x;
    } private__;
    but it’s not like the C compiler will actually enforce privateness. */
};
void C_init_1(C *const inst) {inst->x = 0;}
void C_copy(C *const inst, const C *const c) {inst->x = c->x;}
void C_init_2(C *const inst, int cv) {inst->x = cv;}
int C_getX(const C *const inst) {return inst->x;}

If C extends something, it has to start with a copy of that thing; e.g.,

class D : public E, public F {…};

becomes

typedef struct D D;
struct D {
    E super_E;
    F super_F;
};

so static upcasting from D to E becomes &inst->super_E and the reverse is something like (struct D *)((char *)inst - offsetof(D, super_E)).

Virtual stuff gets into a whole other layer of crazy. A class with virtual members (including inherited ones) becomes something like

typedef struct V V;
struct V__vtable;
struct C {
    const struct V__vtable *vtable__;
    …
};
struct V__vtable {
    (virtual function pointers here)
};

and then you have to figure out a way to do dynamic casting (exercise for the reader:).

Templates usually end up as macros, and there’s really no good way to handle that uniformly; you’ll have to manually come up with names for the template instantiations and make sure things get pasted together properly. They might not be as bad if you use a separate header for each template and take macro args (#undef at end of file), then #include the header whenever you need to instantiate the template.

So you can do it, but it’s almost not worth it if you have any way to just shim into the C++ code from C. (E.g., come up with some extern "C" functions that let you access the C++ side of things.)

2 of 5
10

Depending on the reason for this change you might prefer to do this:

By writing extern "C" in front of a function you can later link it with C code. This will not work for classes or templates, but you might be able to write a wrapper to the stuff you want to use in C from the C++-Code.

I know that for old versions of C++ there used to be some sort of transpilers to C, but not for modern C++ and this would generate very ugly C.

Sorry that this is not a very good answer to your question, but i hope this still helps.

🌐
CodeConverter
codeconverter.io β€Ί convert β€Ί c-to-c-plus-plus
Converting C to C++: A Comprehensive Guide | Convert your code to any language or framework effortlessly.
Learn the essential steps to convert your C code to C++ efficiently. This guide covers the key differences and how to adapt your C projects to C++.
Top answer
1 of 11
16

Having just started on pretty much the same thing a few months ago (on a ten-year-old commercial project, originally written with the "C++ is nothing but C with smart structs" philosophy), I would suggest using the same strategy you'd use to eat an elephant: take it one bite at a time. :-)

As much as possible, split it up into stages that can be done with minimal effects on other parts. Building a facade system, as Federico Ramponi suggested, is a good start -- once everything has a C++ facade and is communicating through it, you can change the internals of the modules with fair certainty that they can't affect anything outside them.

We already had a partial C++ interface system in place (due to previous smaller refactoring efforts), so this approach wasn't difficult in our case. Once we had everything communicating as C++ objects (which took a few weeks, working on a completely separate source-code branch and integrating all changes to the main branch as they were approved), it was very seldom that we couldn't compile a totally working version before we left for the day.

The change-over isn't complete yet -- we've paused twice for interim releases (we aim for a point-release every few weeks), but it's well on the way, and no customer has complained about any problems. Our QA people have only found one problem that I recall, too. :-)

2 of 11
14

What about:

  1. Compiling everything in C++'s C subset and get that working, and
  2. Implementing a set of facades leaving the C code unaltered?

Why is "translation into C++ mandatory"? You can wrap the C code without the pain of converting it into huge classes and so on.

🌐
CodingFleet
codingfleet.com β€Ί code-converter β€Ί c++ β€Ί c
C++ to C Converter - CodingFleet
Convert your C++ Code to C. This exceptional AI-powered tool converts your C++ code into C code easily, eliminating the need for manual re-coding. Save your precious time and unlock cross-platform development like never before with our converter tool.
Find elsewhere
🌐
PTC
ptc.com β€Ί en β€Ί products β€Ί developer-tools β€Ί lex-and-yacc
PTC Lex & YACC Convert Any Language into C or C++ Code | PTC
December 3, 2023 - A powerful program generation tool which processes any language specification you provide into usable, portable, and expandable C or C++ code.
🌐
Sololearn
sololearn.com β€Ί en β€Ί Discuss β€Ί 2985266 β€Ί convert-code-to-c-language
Convert code to C language | Sololearn: Learn to code for FREE!
C structs are plain data carrier, I think you can turn that ShowInfo() into a regular function, allow it to accept a parameter of struct `Course` type, which will be used as source of data to print.
🌐
Javainuse
javainuse.com β€Ί c2cpp
Online C Code to C++ Converter Tool
Online tool to convert C source code into C++. The main difference between C and C++ is that C is function-driven procedural language with no support for objects and classes, whereas C++ is a combination of procedural and object-oriented programming languages.
🌐
Quora
quora.com β€Ί Are-there-any-tools-to-automatically-convert-C-code-to-C++-code
Are there any tools to automatically convert C code to C++ code? - Quora
Yes. Converting C to C++ is usually straightforward for many programs, but there is no single perfect automatic tool that guarantees idiomatic, modern, or safe C++ output for every codebase. Available tools fall into three categories: automated translators (mechanical), compatibility helpers, and assisted migration toolchains that combine static analysis and refactoring.
🌐
Mtsystems
mtsystems.com
mtSystems - C Source Code to Java Source Code Translation
We cannot provide a description for this page right now
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 34289158 β€Ί how-to-convert-c-to-c
How to convert C to C++? - Stack Overflow
A source code that is written in C is already a C++ code. Just change the extension of the file to .cpp ... Not all C is C++! There are differences, but most straightforward C code can be compiled directly as C++. Not sure about this, need to make sense of it first.
🌐
TutorialsPoint
tutorialspoint.com β€Ί online_c_formatter.htm
Online C/C++ Formatter | Tutorialspoint
Online C/C++ Formatter and Beautifier - Try online C/C++ formatter and beautifier and Editor to beautify and format C/C++ code
🌐
Tangiblesoftwaresolutions
tangiblesoftwaresolutions.com
Source Code Converters
Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters
🌐
Code Beautify
codebeautify.org β€Ί alleditor β€Ί cbcae3e9
converter c++ code to c code
This tool helps you to write code with color full syntax and share with others.
🌐
Quora
quora.com β€Ί Can-we-convert-c++-program-to-c-language
Can we convert c++ program to c language? - Quora
Answer (1 of 17): You are in luck, because the same compiler is used for both languages. It will be simpler if you do not include any C++ libraries in your code, including the STL headers. Only use the headers. That is probably not what you are trying to do, however. What is usually done ...