| | |
How do you write a code which compiles in c but not in c++?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>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]
C++ Syntax (Toggle Plain Text)
#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; }
•
•
•
•
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
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)
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
Posts: 4
Reputation:
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.C++ Syntax (Toggle Plain Text)
#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
![]() |
Similar Threads
- 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: missing function header
- Next Thread: Program design tools
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






