•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,491 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,260 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2748 | Replies: 8
![]() |
•
•
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation:
Rep Power: 5
Solved Threads: 2
>How am i supposed to write a code in c which compiles in c but not in c++ That is, one way would be to use a C++ (and not C) keyword as a variable.
[EDIT]
Another way. [/EDIT]
#include <stdio.h>
int main(void)
{
char new[] = "Compiles in C, not in C++";
puts(new);
return 0;
}[EDIT]
Another way.
#include <stdio.h> #ifdef __cplusplus #error Prevented from compiling as C++ #endif int main(void) { puts("Hello world"); return 0; }
•
•
Join Date: Apr 2004
Location: Virginia Beach
Posts: 113
Reputation:
Rep Power: 5
Solved Threads: 2
•
•
•
•
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:
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
Application development, webhosting, and much more: www.webcentric-hosting.com
•
•
Join Date: May 2004
Location: Egypt - Cairo
Posts: 129
Reputation:
Rep Power: 5
Solved Threads: 2
•
•
Join Date: May 2004
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by The Other Dave
>How am i supposed to write a code in c which compiles in c but not in c++That is, one way would be to use a C++ (and not C) keyword as a variable.#include <stdio.h> int main(void) { char new[] = "Compiles in C, not in C++"; puts(new); return 0; }
[EDIT]
Another way.[/EDIT]#include <stdio.h> #ifdef __cplusplus #error Prevented from compiling as C++ #endif int main(void) { puts("Hello world"); return 0; }
Is that all?
Or r there any more?
Can u give me if any plz
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- how do i write the code for taking out a hard copy of the web page (HTML and CSS)
- Write C++ Code (C++)
- Can we Programaticall Write code in VB6.0, forms/standard module/class code window (Visual Basic 4 / 5 / 6)
- Could somebody write code for my project plz... (Visual Basic 4 / 5 / 6)
- Could Somebody Write Code Plz... (Visual Basic 4 / 5 / 6)
- I need to write a C++ Code from the following info. HOW DO I... (C++)
Other Threads in the C++ Forum
- Previous Thread: problems with reading random access line from a file
- Next Thread: I have a few questions to be patched up can anybody get these to me ?



Linear Mode