How do you write a code which compiles in c but not in c++?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2004
Posts: 4
Reputation: prabhu_kar is an unknown quantity at this point 
Solved Threads: 0
prabhu_kar prabhu_kar is offline Offline
Newbie Poster

How do you write a code which compiles in c but not in c++?

 
0
  #1
May 6th, 2004
How am i supposed to write a code in c which compiles in c but not in c++
plz do let me know
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: How do you write a code which compiles in c but not in c++?

 
0
  #2
May 6th, 2004
c++ is a subset of c, or maybe it's the otehr way round i 4got that crap, but anyhow, piont is, u can't.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: How do you write a code which compiles in c but not in c++?

 
0
  #3
May 6th, 2004
This Right YOu can't do that coz C is subset of C++ ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How do you write a code which compiles in c but not in c++?

 
0
  #4
May 6th, 2004
>How am i supposed to write a code in c which compiles in c but not in c++
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. char new[] = "Compiles in C, not in C++";
  6. puts(new);
  7. return 0;
  8. }
That is, one way would be to use a C++ (and not C) keyword as a variable.

[EDIT]
Another way.
#include <stdio.h>
 
#ifdef __cplusplus
#error Prevented from compiling as C++
#endif
 
int main(void)
{
puts("Hello world");
return 0;
}
[/EDIT]
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: How do you write a code which compiles in c but not in c++?

 
0
  #5
May 6th, 2004
LOL good thinking
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: How do you write a code which compiles in c but not in c++?

 
0
  #6
May 6th, 2004
Hi,

C++ is a superset of C.

I can only wonder WHY you would want to try to compile something that works with C only? That doesn't make sense.

Christian
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 113
Reputation: liliafan is on a distinguished road 
Solved Threads: 2
liliafan's Avatar
liliafan liliafan is offline Offline
Junior Poster

Re: How do you write a code which compiles in c but not in c++?

 
0
  #7
May 7th, 2004
Originally Posted by kc0arf
Hi,

C++ is a superset of C.

I can only wonder WHY you would want to try to compile something that works with C only? That doesn't make sense.

Christian
I seemed to remember reading about some incompatibilities that were valid C and not C++ code after doing a few minutes googling I found this link:

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

Whilst most of the incompatibilities exist only in theory between the ISO99 standard C and C++ there does exist some problems such as:

  1. extern int foo();
  2.  
  3. void main()
  4. {
  5. foo(1,2);
  6. }

Is valid C but will cause an error in C++, in 2 places with g++ since main must return an int, and there is too many arguements passed in foo, in C it is assumed that extern int foo() calls a function with an unspecified number of arguements where as C++ assumes it is called a function with no arguements ie: extern int foo(void)

So whilst there shouldn't be incompatibilities some do exist due to language evolution.

Btw most of the incompatibilities on the link included are theoretical also with some compilers the incompatibility I have shown will be detected with others it won't according to the standard it is invalid C++ but valid C, and when I tested this will gcc and g++ it showed the problem.

HTH
Ben
Application development, webhosting, and much more: www.webcentric-hosting.com
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: How do you write a code which compiles in c but not in c++?

 
0
  #8
May 7th, 2004
LOL .. Nice
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 4
Reputation: prabhu_kar is an unknown quantity at this point 
Solved Threads: 0
prabhu_kar prabhu_kar is offline Offline
Newbie Poster

Re: How do you write a code which compiles in c but not in c++?

 
0
  #9
May 11th, 2004
Originally Posted by The Other Dave
>How am i supposed to write a code in c which compiles in c but not in c++
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. char new[] = "Compiles in C, not in C++";
  6. puts(new);
  7. return 0;
  8. }
That is, one way would be to use a C++ (and not C) keyword as a variable.

[EDIT]
Another way.
#include <stdio.h>
 
#ifdef __cplusplus
#error Prevented from compiling as C++
#endif
 
int main(void)
{
puts("Hello world");
return 0;
}
[/EDIT]


Is that all?
Or r there any more?
Can u give me if any plz
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC