How am i supposed to write a code in c which compiles in c but not in c++
plz do let me know

Recommended Answers

All 8 Replies

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.

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

>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]

LOL good thinking :)

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

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

LOL .. Nice :)

>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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.