| | 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 |
|
|
well c and c++ are pretty much the same i think maybe
and 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.
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.
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.
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.
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
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
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.
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++... |
|
![]() C Or C++ |
| ADD REPLY / Got an Opinion! | a humble request :-) | RAPID SEARCH! | Free Hosting | [X] |
|
Express your Opinions, Thoughts or Contribute more info. to help others. Ask your Doubts & Queries to get answers, So that "Together We can help others!" |
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP. | 500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE |
|