954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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++
plz do let me know

prabhu_kar
Newbie Poster
4 posts since May 2004
Reputation Points: 7
Solved Threads: 0
 

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.

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 

This Right YOu can't do that coz C is subset of C++ ...

meabed
Junior Poster
Team Colleague
139 posts since May 2004
Reputation Points: 55
Solved Threads: 3
 

>How am i supposed to write a code in c which compiles in c but not in c++

#include <stdio.h>
 
int main(void)
{
char new[] = "Compiles in C, not in C++";
puts(new);
return 0;
}


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]

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

LOL good thinking :)

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 

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

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

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:

extern int foo();

void main()
{
   foo(1,2);
}


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

liliafan
Junior Poster
117 posts since Apr 2004
Reputation Points: 66
Solved Threads: 3
 

LOL .. Nice :)

meabed
Junior Poster
Team Colleague
139 posts since May 2004
Reputation Points: 55
Solved Threads: 3
 

>How am i supposed to write a code in c which compiles in c but not in c++

#include <stdio.h>
 
int main(void)
{
char new[] = "Compiles in C, not in C++";
puts(new);
return 0;
}

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

prabhu_kar
Newbie Poster
4 posts since May 2004
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You