Jump to content

How to convert C to C++ code ?

- - - - -

  • Please log in to reply
11 replies to this topic

#1
delphiproblematic

delphiproblematic

    Newbie

  • Members
  • PipPip
  • 11 posts
Does anybody know anyway ?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
99% of C code is valid C++ code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
delphiproblematic

delphiproblematic

    Newbie

  • Members
  • PipPip
  • 11 posts
cout , cin different with printf scanf :(

#4
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

delphiproblematic said:

cout , cin different with printf scanf :(
Even printf and scanf will work on C++... if you haven't know that, then do try them and check it...

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

delphiproblematic said:

cout , cin different with printf scanf :(
printf and scanf are valid C++ functions. This is an issue of standard style, rather than valid code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
The largest problem converting c to c++ is typecasting void* return values, such as the return value of malloc(). c++ requires typecast, c does not.

You might also want to read some of these links, which discuss the problems you might encounter.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7
instantcake

instantcake

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
only bad coding is a problem when converting c to c++. the truth is c++ is just better, and has many functions that replace c functions. more than 99% of your c code is valid, but some purists hate seeing c code in c++ programs. bottom line is it really doesn't matter if you want to update it with new functions or not its up to you.

#8
omega

omega

    Newbie

  • Members
  • PipPip
  • 16 posts
C++ = C + V + OOP

simple, not ?

from .c works to .cpp there are some changes:

1. C-STYLE structures are not supported
2. C-STYLE MACRO is not supported (you might never use this these days)

and to work that out you go on with these steps:

1. put all of your C codes into functions like this,
#ifdef __cpp__
  extern "C" {
#endif

 // C  functions OR code

#ifdef  __cpp__
  };
#endif


#9
instantcake

instantcake

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

omega said:

C++ = C + V + OOP

simple, not ?

from .c works to .cpp there are some changes:

1. C-STYLE structures are not supported
2. C-STYLE MACRO is not supported (you might never use this these days)

and to work that out you go on with these steps:

1. put all of your C codes into functions like this,
#ifdef __cpp__
  extern "C" {
#endif

 // C  functions OR code

#ifdef  __cpp__
  };
#endif
acctually this means that you have to qualify everything in the global namespace using the :: operator if you use using namespace std; AND that is inaccurate you CAN use c structures. C++ extends C and does not take out using structures, it just makes them Public by default, and they can contain functions

#10
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts

omega said:

1. C-STYLE structures are not supported

Wrong. structures are still supported.

omega said:

2. C-STYLE MACRO is not supported (you might never use this these days)
wrong again.


omega said:

1. put all of your C codes into functions like this,
#ifdef __cpp__
  extern "C" {
#endif

 // C  functions OR code

#ifdef  __cpp__
  };
#endif

That's only necessary if you want to call the C functions compiled as C code from functions compiled as c++ code. When you compile the c code with c++ compiler then that is not necessary.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#11
instantcake

instantcake

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

Ancient Dragon said:

Wrong. structures are still supported.


wrong again.




That's only necessary if you want to call the C functions compiled as C code from functions compiled as c++ code. When you compile the c code with c++ compiler then that is not necessary.
exactly my point

#12
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts

omega said:

1. C-STYLE structures are not supported
2. C-STYLE MACRO is not supported (you might never use this these days)

and to work that out you go on with these steps:

1. put all of your C codes into functions like this,
#ifdef __cpp__
  extern "C" {
#endif

 // C  functions OR code

#ifdef  __cpp__
  };
#endif
Yeah, that's pretty much plain wrong, sorry. In addition to what AD said,
this:
#ifdef __cpp__
  extern "C" {
#endif

 // C  functions OR code

#ifdef  __cpp__
  };
#endif
should be
#ifdef __cpp__
  extern "C" {
#endif

 // C  functions OR code

#ifdef  __cpp__
  }
#endif

Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users