Can someone provide TIPS in order to convert C++ code into C code?

This is a discussion on Can someone provide TIPS in order to convert C++ code into C code? within the C++ Programming forums, part of the General Programming Boards category; Hi All, I want to convert C++ code into C code; I have following questions in specific: 1. Ok.. I ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Can someone provide TIPS in order to convert C++ code into C code?

    Hi All,

    I want to convert C++ code into C code; I have following questions in specific:

    1. Ok.. I will convert classes into structs, but do structs in C contain functions? If yes, are functions declared and defined in struct body itself? Also, what to do if I want to declare func in struct but define outside it?

    2. How to replace operator overloading kind of thing? Will struct vars behave just like class objects?

    3. How to replace object handling kinda stuff? E.g. If I have an object of a class as member of some other class in C++, will simply replacing two classes with two diff. structures suffice? And in that order, struct vars. of these 2 diff. classes will behave like objects behaved in C++?


    PLEASE HELP and ADVISE.


    If you have other tips or some useful web links, please let me know.


    THANKS IN ADVANCE!


    Cheers.
    TC.

    --Rohit

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by rohit99 View Post
    1. Ok.. I will convert classes into structs, but do structs in C contain functions? If yes, are functions declared and defined in struct body itself? Also, what to do if I want to declare func in struct but define outside it?
    No. Structs can't contain functions.
    Welcome to C.

    2. How to replace operator overloading kind of thing? Will struct vars behave just like class objects?
    User-defined functions that you call (explicitly), of course.
    There are no "objects" in C. They will behave like variables. No more, no less.

    3. How to replace object handling kinda stuff? E.g. If I have an object of a class as member of some other class in C++, will simply replacing two classes with two diff. structures suffice? And in that order, struct vars. of these 2 diff. classes will behave like objects behaved in C++?
    Yes and no. Yes, it will work, but it might not work depending on what you do.
    Overloaded operators will stop working, for example.
    Private/public/protected will stop working.
    Etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Converting C++ code into C code is a huge pain in the ass as Elysia explained.
    Do you really NEED to convert to C? What is your reason for doing so?

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Hi Elysia and cpjust,

    Thanks for your replies..

    well, I was not too keen to convert C++ code into C code.. but my teacher says compiled C code is less in size compared to C++ code.. i dont knw if it's true.. i have read it depends on the compiler.. n generally it shouldn't be true but specifically it somehow comes out to be true..

    n the code I wanna convert is cryptographic algo..


    One more thing.. can you plz explain with example how C++ class function will be bound to a particular struct even when func is not declared or defined in it??

    Do you guys have more pieces of advice?


    --Rohit

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by rohit99 View Post
    well, I was not too keen to convert C++ code into C code.. but my teacher says compiled C code is less in size compared to C++ code.. i dont knw if it's true.. i have read it depends on the compiler.. n generally it shouldn't be true but specifically it somehow comes out to be true..

    n the code I wanna convert is cryptographic algo..
    So what does it matter?

    One more thing.. can you plz explain with example how C++ class function will be bound to a particular struct even when func is not declared or defined in it??
    It isn't, it won't and it can't. Usually, a function would simply take a pointer to the actual struct.

    Do you guys have more pieces of advice?
    If possible, don't convert C++ code into C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    well, I was not too keen to convert C++ code into C code.. but my teacher says compiled C code is less in size compared to C++ code.. i dont knw if it's true.. i have read it depends on the compiler.. n generally it shouldn't be true but specifically it somehow comes out to be true..
    Hand crafted assembly language code can result in even smaller executables compared to C code.

    Doing everything in hardware results in executables of zero size, and in fact the AES algorithm was selected partly due to ease of implementation in hardware.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    That is strange I can compile hello world program in C with G++(release build) and get about a 35kb file. As with Visual C++ 2008, C++ hello world my File size is about 6kb. I don't think the language really effects the size of the executable.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    C++ could bloat the size of executables a bit, especially with templates. It depends on the code and the compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > but my teacher says compiled C code is less in size compared to C++ code
    If your C++ code is fairly simple (none of the inheritance / polymorphism / operator overloading etc) which has no good equivalent in C, then a simple mechanical translation of

    foo::func ( var )
    to
    foo_func( &instanceOfFoo, var )

    isn't going to save you a great deal, as it's what C++ does anyway.

    One wonders what to say if your C answer ends up bigger than the C++. Does that prove or disprove the tutor's assertion?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Smile

    Thanks guyz...........

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is my opinion that C++ and C code performing the same task, written in efficient code, will not be hugely different in performance and size.

    It is perfectly possible to write inefficient and unnecessarily large code in both C and C++ - and I guess C++ can HIDE the complexity of a complicated piece of code - but in the end, someone who knows what he/she is doing should be able to produce good, efficient code in both languages, and unless you have good evidence that the code is inefficient, translating an encryption algorithm from C++ to C will just make your life hard, without much benefit.

    The task of translating C++ to C when it's heavily relying on C++ functionality can be quite hard, as has been stated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    129
    What might be interesting is to try to find an old compiler that converts C++ to C prior to compilation, which is how it used to be done. I think Objective C is still done this way by some compilers.
    http://users.pandora.be/stes/compiler.html

    You might be able to find something similar for C++.

  13. #13
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question

    Quote Originally Posted by Elysia View Post
    No. Structs can't contain functions.
    Welcome to C.
    But they can contain function pointers can't they?

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by A10 View Post
    But they can contain function pointers can't they?
    That's right.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by SevenThunders View Post
    What might be interesting is to try to find an old compiler that converts C++ to C prior to compilation, which is how it used to be done. I think Objective C is still done this way by some compilers.
    http://users.pandora.be/stes/compiler.html

    You might be able to find something similar for C++.
    Yes, you'd be looking for CFront - which is the "first" C++ compiler - it was a front end that basically pre-processed the C++ file into C.

    However, there is absolutely no benefit in this, as it will just produce the same (or sligthly worse) binary as the corresponding native [1] C++ compiler. It would be slightly worse in cases where a native C++ compiler can do optimizations that are not visible once the code has been translated to C - for example knowing the type of an object and calling a virtual function without the vtable would not be possible in C, since that information has been lost.

    Also, you probably can't expect CFront's output to be "nice and readable" C code (although I have never looked at it closely).

    [1] Native here means a C++ compiler that takes C++ code and directly generates assembler/machine code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  2. Re : convert character into special code
    By Wah in forum C Programming
    Replies: 0
    Last Post: 01-30-2002, 11:55 PM
  3. Replies: 4
    Last Post: 01-15-2002, 11:04 PM
  4. Convert code to use Pointers, Functions, etc.
    By JYoung in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-13-2001, 12:33 PM
  5. how convert english to morse code
    By uler in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2001, 07:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21