hamza1405241548 0 Report post Posted July 22, 2007 i have recently add this hello world code into a c++ compiler and c compiler and it works in both:#include<iostream.h>main(){ cout<<"hello world";}Can any one explain why this happened Share this post Link to post Share on other sites
Sten 0 Report post Posted July 22, 2007 well c and c++ are pretty much the same i think maybeand something that simple probably just works in both.do something more advanced and try it in the c and c++ compiler to see if that works in both. Share this post Link to post Share on other sites
hamza1405241548 0 Report post Posted July 22, 2007 (edited) i have tried many programs and find out that when you do not add "using namespace std" most basic programs work in both compilers except classes. Edited July 22, 2007 by hamza (see edit history) Share this post Link to post Share on other sites
pyost 0 Report post Posted July 22, 2007 C was created prior to C++, as you might have expected, and thus has lesser capabilities. More precisely, C++ is an object-oriented programming language, while C isn't. Therefore, it would be logical for C++ to be able to do everything C can, but add more functionality at the same time.Now, I am not acquainted with either of these, but I can say with a great amount of certainty that classes are what make C++ an object-oriented language. A majority of languages used today are object-oriented, so C++ has an obvious advantage to C. Share this post Link to post Share on other sites
nikhil1405241522 0 Report post Posted July 22, 2007 (edited) Please check the extension of the program file wether you have put it as ".C" or ".CPP". If you have put it as ".CPP" then it is quite natural as almost all the compilers can compile C as well as CPP programs ... Â But if thats not the case then the compiler you are using is not a good one. A compiler is said to be erroneous if it compiles the program written in any other language other than what it is made for. Â Dont get the wrong idea that if a compiler can compile both C and CPP programs than it is not a good one ... but it should compile only one language at a time. Edited July 22, 2007 by nikhil (see edit history) Share this post Link to post Share on other sites
hamza1405241548 0 Report post Posted July 23, 2007 actually i have two comilers and the program i write is .cpp. i use both c and c++ and notice this point. thanks for giving me good explaination Share this post Link to post Share on other sites
rcparur 0 Report post Posted July 23, 2007 Actually C++ evolved from C, therefore C++ is a Superset of C. In other words, code written in C will compile in C++, but not necessarily the other way round.In the sample you have written the statement starting with "cout" is definitely C++ as cout is an Object. Only Object oriented languages support Objects. C++ is object oriented and C is not.The rest of the sample is in C. But as mentioned earlier, it will work in C++ also.But todays compilers for C and C++ are so closely related that it is sometimes difficult for beginners to see where C ends and C++ starts. Also for the purposes of programming, it really does not matter, you can Mix C and C++ code in a program and the compiler will compile it and it will run perfectly ( if the code is written correctly Share this post Link to post Share on other sites
Jeigh1405241495 0 Report post Posted July 23, 2007 Exactly, as stated a c++ compiler can, will, and SHOULD be able to compile c code without problem. Since c++ contains all of c, plus all the things special to c++ it is logical to assume one compiler can compile both c and c++. Now, c++ code shouldn't be able to be compiled in a c compiler since its lacking the capabilities to do so.If you really care, you should get a super super basic c compiler and try it out to see the problems that come when you compile c++ code in a c compiler. I imagine some c compilers are actually given the c++ compiling capabilities now just in case. Share this post Link to post Share on other sites
The Pixel Coder 0 Report post Posted January 12, 2008 Your code is 100% C++, not C.I understand your statement, you've probably used a compiler, like gcc, which compiles both C and C++... Share this post Link to post Share on other sites