|
|
|
|
![]() ![]() |
Jul 22 2007, 07:33 AM
Post
#1
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 23 Joined: 21-July 07 Member No.: 23,495 |
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 |
|
|
|
Jul 22 2007, 07:42 AM
Post
#2
|
|
|
Oh come on Mrs. B! Group: Members Posts: 648 Joined: 6-June 07 From: Tasmania, Australia Member No.: 22,422 |
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. |
|
|
|
Jul 22 2007, 02:38 PM
Post
#3
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 23 Joined: 21-July 07 Member No.: 23,495 |
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.
This post has been edited by hamza: Jul 22 2007, 02:39 PM |
|
|
|
Jul 22 2007, 05:39 PM
Post
#4
|
|
|
Nenad Bozidarevic Group: [MODERATOR] Posts: 1,002 Joined: 7-November 05 From: Belgrade, Serbia Member No.: 9,500 |
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. |
|
|
|
Jul 22 2007, 07:03 PM
Post
#5
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 17 Joined: 10-May 06 Member No.: 13,334 |
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. This post has been edited by nikhil: Jul 22 2007, 07:04 PM |
|
|
|
Jul 23 2007, 08:30 AM
Post
#6
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 23 Joined: 21-July 07 Member No.: 23,495 |
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
|
|
|
|
Jul 23 2007, 11:13 AM
Post
#7
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 5 Joined: 20-July 07 Member No.: 23,475 |
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 |
|
|
|
Jul 23 2007, 11:55 AM
Post
#8
|
|
|
Whitest Black Mage Group: [MODERATOR] Posts: 1,323 Joined: 20-May 05 From: NB, Canada Member No.: 5,281 |
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. |
|
|
|
Jan 12 2008, 10:28 PM
Post
#9
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 12 Joined: 12-January 08 Member No.: 27,570 |
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++... |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 6th September 2008 - 01:19 AM |