Solved

how to convert c++ to c

Posted on 2004-04-15
18
1,133 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
  • 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:mayankeagle
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
 
LVL 30

Expert Comment

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

Expert Comment

by:mayankeagle
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:mayankeagle
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now