Tuesday, July 14, 2009

Converting a program from C++ to C?

Basically I have a C++ program that I am trying to convert into C. I have no knowledge of C but from what I have gathered on the internet I have made a few changes to my program. However, I am assuming that my program does not compile with C. Therein lies my other issue; I am using a windows XP operating device with a C++ developer software, but I have no compiler for C.





Does anyone know where I could find a compiler for C so that I can check my code and make sure it compiles correctly?





Also, in Additional Details I will provide my program and if you can spot anything that would utterly fail in C, please let me know. Any advice and help is greatly appreciated.

Converting a program from C++ to C?
You assume incorrectly. There is nothing in the code posted here that needs to be converted. There a couple of omissions through.





#include %26lt;stdio.h%26gt;





double convertTemp(double);


void printDate(char []);








int main()


{


const int SIZE = 10; /*change this to whatever*/





char type[SIZE];


double sum = 0;


int count = 0;


double test = 0;


double temp[SIZE]; /*fill in before calling convert */


char date[12]; /*fill in before you print*/





printDate(date);


convertTemp(test);


}











double convertTemp(double temperature)


{





double celsius = 0;


celsius = (5.0/9)*(temperature-32);


return celsius;





}








void printDate(char date[])


{


printf("%s\n", date); /*print statement */


}
Reply:If you have C++ code and a C++ compiler...why not just keep it as C++?
Reply:Your code will compile and run in standard C except that it won't do anything useful :-P





Good free C compilers:


MinGW - http://www.mingw.org/


Pelles C - http://www.smorgasbordet.com/pellesc/


Open Watcom - http://www.openwatcom.org/
Reply:I attached a link for a c compiler. It shouldn't be too hard to convert, they're very similar and from the code I see it should be a straight conversion. You may have to change a few things. functions are declared a little differently in C (but very similar). Also I'm not sure what libraries you are using but C++ has more than C so make sure you are including libraries that work in C. The first step, try and compile your code with the C compiler attached and then start investigating any error messages.


No comments:

Post a Comment