Friday, July 31, 2009

Can u compile c pgm in c++ compiler?

Sometimes, but not always because C++ is not a complete super set of C language, all C language code is not valid in C++. There are lots of differences between C and C++, read:


http://david.tribble.com/text/cdiffs.htm





Here are some small examples which compiles in C and would fail to compile in C++:





int main()


{


int class;


class=10;


printf("%d",class);


}





This will not compile under C++ because class is a keyword, but will compile in C without any problems. Similarly any keywords which are specific to C++ such as public, private, virtual, friend etc can be used as identifiers in C, but not in C++.





int fun()


{


//some code


}


int main()


{


fun(10,20);


}





This will fail to compile in C++, because in C++, the declaration of a function with no parameter list is equivallent to declaring it as function with void parameter list. But in C language, it means a function with unspecified number of paramerters and we can pass any number of arguments to this function in C.











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


int main()


{


int *array=malloc(sizeof(int)*100);


}





This is valid in C because a pointer of type void* can be assigned to any other pointer without cast, but this is not the case with C++ and we will have to give an explicit cast to it as:


int *array=(int*)malloc(sizeof(int)*100);

Can u compile c pgm in c++ compiler?
yes u can compile. C++ is based on c only know. So c++ compiler will very well know about ur c syntax.
Reply:Yes we can
Reply:yes . You can compile a c pgm in a c++ compiler assuming u have the std libraries..
Reply:Yes. you can do.


But it also depends on the C++ compiler you are using.


Turbo C++ compiler allows that.


Some C++ compilers dont allow that and the yrequire strict C++ syntax and libraries.


No comments:

Post a Comment