943,900 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4689
  • C++ RSS
May 6th, 2004
0

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

Expand Post »
How am i supposed to write a code in c which compiles in c but not in c++
plz do let me know
Similar Threads
Reputation Points: 7
Solved Threads: 0
Newbie Poster
prabhu_kar is offline Offline
4 posts
since May 2004
May 6th, 2004
-1

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

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.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
May 6th, 2004
0

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

This Right YOu can't do that coz C is subset of C++ ...
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
May 6th, 2004
1

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

>How am i supposed to write a code in c which compiles in c but not in c++
C++ Syntax (Toggle Plain Text)
  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]
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 6th, 2004
0

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

LOL good thinking
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
May 6th, 2004
-2

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

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
May 7th, 2004
0

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

Quote 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:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
May 7th, 2004
0

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

LOL .. Nice
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
May 11th, 2004
0

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

Quote originally posted by The Other Dave ...
>How am i supposed to write a code in c which compiles in c but not in c++
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 7
Solved Threads: 0
Newbie Poster
prabhu_kar is offline Offline
4 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Question regarding reading from registry (Win32) VC6
Next Thread in C++ Forum Timeline: Program design tools





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC