Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to convert c++ to c

Posted on 2004-04-15
18
Medium Priority
?
1,180 Views
Last Modified: 2008-03-10
can anyone tell me how to convert a c++ program to c
thank you...
0
Comment
Question by:kalaichris11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +5
18 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10831289
Hi kalaichris11,

check this link
http:20954756.html

Sunnycoder
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10832240
Sunny, the link you gave doesn't open. Perhaps the question was in some other TA.

BTW, kalaichris11, can you post some code? That would help us help you better for this specific problem because its not simple to make a complete summary of how to conver C++ code to C code (it won't be a conversion actually - it'll involve a lot of re-writing and designing - things like operator overloading, polymorphism, inheritance will have to be dealt with).
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10832302
Thanks mayank .... I had forgotten the Q_ and that was the cause of problem ...
This is the second time this week that  I have formed a bad link ... doh
http:Q_20954756.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 10832334
Oops, it was too bit of me also not to realize that Q_ is missing ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10832383
>> This is the second time this week that  I have formed a bad link ... doh

Ah, take it easy ;-) I just copy-paste the entire URL from the browser's address-box. BTW, just out of the blue: how do page-editors and clean-up volunteers figure out the individual comment-IDs for the comments on a page?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10832835
We have slightly different view from expert mode .... Comment id is visible in our view

CVs post using a special tool ... that tool automatically inserts the comment id ... All a CV has to do is select the comment
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10832868
Hmmm, I guessed it might be something of that sort, thanks ;-)
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 10834812
You can't do that.

C++ is a superset of C. You can convert C to C++ by simply renaming the file from .c to .cpp or .cxx or .whatever_your_c++_extension_is, and compile it with the C++ compiler.

What you can do is provide Hooks in your C++ so that your C code can call the C++ functions. You do this using the extern "C" mechanism. The reason for this is that C++ can have different functions with the same name(but diff arg lists).

eg

C++:

int whatever(void);
int whatever(int id);
int whatever(char *name);
int whatever(int id, char *name, char *address="addr");

Your C++ Code will call whichever function fits. If you pass no params the first one will be called. If you pass an int the second, if you pass a string the third one, and if you pass 2 or 3 params, an int, a string, and an optional string the 4th one will be called.

In C if you want different function argument lists you need to use a different name for each one.

This is usually done by convention. Eg Open gl uses function_name3F to take a function with three floating point values.

If you have mixed params you could perhaps suffix your code with a character to represent the type-vals.

eg

extern "C" int whatever_iss(int id, char *name, char *addr){
      return whatever(id,name,addr);
}

iss = int, string, string

Just a suggestion.
0
 
LVL 3

Expert Comment

by:dtkerns
ID: 10834866
I haven't looked at the above links, and I appoligize if they just point you to cfront ... but you can get a copy of cfront and convert C++ into C. C++ was originally written in C, (funny thing is C was originally written in C too) and although C++ APPEARS to be a superset C, anything you can do in C++, can be done in C.
0
 
LVL 24

Expert Comment

by:SunBow
ID: 10837472
The best way is to not use non-C in the first place.
Then, when it comes to platforms, there's no (complete) way to convert C on one, to C on another, or any other options such as C++ to C++. Just a goal, for which solutions are partial. IMHO
0
 
LVL 11

Expert Comment

by:avizit
ID: 10839608
Edison Design Group has a C++ front end

http://www.edg.com/cpp.html


--excerpts from the link --
Also included: a C-generating back end, which can be used to generate C code for C++ programs;
----

The front end is available for free for research pupose. You have to sign a non disclosure aggrement  to get the source.

/abhijit/
0
 
LVL 11

Expert Comment

by:avizit
ID: 10839622
>>although C++ APPEARS to be a superset C, anything you can do in C++, can be done in C. <<

exactly ,  C is low level enough to do anything that can be done by any other language ( except possibly assembly)

/abhijit/
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 10860890
When I said that C++ was a superset of C, I meant that in the constructs of the language were a superset of C's. I did not mean that its capabilities were a superset of C's.

The Styles of programming available to you when using C++ are a superset of those available to you when using C. You can mimic OOP in C using the GObject Library (as GTK does).

You can't call two functions the same name in C. You can in C++, and that is where the conversation from C++ to C becomes a problem. If you have two functions called do_whatever in C++ what will you call it when converted into C. C++ could be converted by hand to C, if you employ the conventions I mentioned above. It could probably even be automaticly converted, if you could tell the converter what conventions to use, an example being the ones used for in OpenGL.

C++ Can be converted to assembly if you want, using g++ just pass the -S flag to the compiler.

Take a look at the assembley code to see what you are up against.

I think your best hope is to compile your C++ code with a C++ compiler and use extern "C" to provide links for your C Code to hang on to.
0
 
LVL 3

Expert Comment

by:dtkerns
ID: 10861391
here's a C example of 2 functions with the same name ...

$ cat hdr.h
struct x {
        void (*func)();
};

struct y {
        void (*func)();
};

$ cat fx.c
#include "hdr.h"

static void f1(void)
{
        printf("I'm called f1(), I'm for use with structs of type x\n");
}

init_x(struct x *a)
{
        a->func = f1;
}

$ cat fy.c
#include "hdr.h"

static void f1(void)
{
        printf("I'm called f1(), I'm for use with structs of type y\n");
}

init_y(struct y *a)
{
        a->func = f1;
}

$ cat main.c
#include "hdr.h"

main()
{

struct x X;
struct y Y;

init_x(&X);
init_y(&Y);

(*X.func)();
(*Y.func)();
}

$ gcc main.c fx.c fy.c

$ ./a.out
I'm called f1(), I'm for use with structs of type x
I'm called f1(), I'm for use with structs of type y

0
 
LVL 6

Expert Comment

by:aolXFT
ID: 10863098
Nice example of pointers to functions, and good demonstration of scopes, how the linker works, etc.

I don't however believe it does anything to deal with the matter. C++ allows you to define two functions with the same name, and call them using the same name, provided they take different parameters. You give nether function a parameter and you call them using two different names, namely X.func, and Y.func.
0
 
LVL 2

Expert Comment

by:colmcc
ID: 10924714
Hi  kalaichris11,

Your question has sparked quite a debate!  I can't resist joining in. :-)

I think the key question which nobody has asked you is - Do you want to convert a given C++ program into C so that you can then go on to maintain that C-code as if it had been written in C to begin with, or do you just want to be able to periodically generate C from your C++ so that, for example, it can then be compiled on some system with a C compiler but no C++ compiler?

Both are possible in theory, but the former would be fiendishly difficult in practice. After all, you could equally-well generate assembly-language from C-code and then try and maintain that!

Incidentally, I believe that the earliest C++ compilers could only generate C-code as their final output, and a separate C compiler had to be used to generate the executable.  This made debugging tricky, as the source-code being debugged was not the code that the programmer wrote.  Thankfully, those days are long gone.  From what others have said above though, it would appear that the tools to do this still exist. (And presumably have been updated for the latest ANSI C++ standard.)

Colin.  


0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11248153
PAQed - no points refunded (of 20)

modulo
Community Support Moderator
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question