fork download
  1. /* A bunch of code that compiles and runs under C89 but fails under any C++
  2.  *
  3.  *
  4.  * Ryan Haining
  5.  * Source may be redistributed freely for educational purposes as long as it
  6.  * contains this notice.
  7.  * If you're redistributing for commerical purposes, err, you may want to
  8.  * rethink that decision.
  9.  */
  10.  
  11. /* type aliases and struct names occupy separate namespaces in C, not in C++ */
  12. struct S { int i; };
  13. typedef int S;
  14.  
  15.  
  16. struct Outer { struct Inner { int i; } in; };
  17. /* struct Inner will be Outer::Inner in C++ due to name scope */
  18. struct Inner inner;
  19.  
  20.  
  21. /* default return type of int in C, C++ functions need explicit return types */
  22. g() {
  23. return 0;
  24. }
  25.  
  26.  
  27. /* C sees this as two declarations of the same integer,
  28.  * C++ sees it as redefinition */
  29. int n;
  30. int n;
  31.  
  32.  
  33. /* K&R style argument type declarations */
  34. void h(i) int i; { }
  35.  
  36.  
  37. /* struct type declaration in return type */
  38. struct S2{int a;} j(void) { struct S2 s = {1}; return s; }
  39.  
  40.  
  41. /* struct type declaration in argument, stupid and useless, but valid */
  42. /*void dumb(struct S3{int a;} s) { } */
  43.  
  44.  
  45. /* enum/int assignment */
  46. enum E{A, B};
  47. enum E e = 1;
  48.  
  49.  
  50. void k() {
  51. goto label; /* C allows jumping past an initialization */
  52. {
  53. int x = 0;
  54. label:
  55. x = 1;
  56. }
  57. }
  58.  
  59.  
  60. /* () in declaration means unspecified number of arguments in C, the definition
  61.  * can take any number of arguments,
  62.  * but means the same as (void) in C++ (definition below main) */
  63. void f();
  64.  
  65. int main (void) {
  66. f(1); /* doesn't match declaration in C++ */
  67. {
  68. /* new is a keyword in C++ */
  69. int new = 0;
  70. }
  71.  
  72. /* no stdio.h include results in implicit definiton in C. However,
  73.   * as long as a matching function is found at link-time, it's fine.
  74.   * C++ requires a declaration for all called functions */
  75. puts("C is not C++");
  76.  
  77. {
  78. int *ip;
  79. void *vp = 0;
  80. ip = vp; /* cast required in C++, not in C */
  81. }
  82.  
  83.  
  84.  
  85. return 0;
  86. }
  87.  
  88. /* matches declaration in C, not in C++ */
  89. void f(int i) { }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:13:13: error: conflicting declaration ‘typedef int S’
 typedef int S;
             ^
prog.cpp:12:8: error: ‘struct S’ has a previous declaration as ‘struct S’
 struct S { int i; };
        ^
prog.cpp:18:14: error: aggregate ‘Inner inner’ has incomplete type and cannot be defined
 struct Inner inner;
              ^
prog.cpp:22:3: error: ISO C++ forbids declaration of ‘g’ with no type [-fpermissive]
 g() {
   ^
prog.cpp:30:5: error: redefinition of ‘int n’
 int n;
     ^
prog.cpp:29:5: error: ‘int n’ previously declared here
 int n;
     ^
prog.cpp:34:9: error: variable or field ‘h’ declared void
 void h(i) int i; { }
         ^
prog.cpp:34:8: error: ‘i’ was not declared in this scope
 void h(i) int i; { }
        ^
prog.cpp:34:18: error: expected unqualified-id before ‘{’ token
 void h(i) int i; { }
                  ^
prog.cpp:38:1: error: new types may not be defined in a return type
 struct S2{int a;} j(void) { struct S2 s = {1}; return s; }
 ^
prog.cpp:38:1: note: (perhaps a semicolon is missing after the definition of ‘S2’)
prog.cpp:47:12: error: invalid conversion from ‘int’ to ‘E’ [-fpermissive]
 enum E e = 1;
            ^
prog.cpp: In function ‘void k()’:
prog.cpp:54:1: error: jump to label ‘label’ [-fpermissive]
 label:
 ^
prog.cpp:51:10: error:   from here [-fpermissive]
     goto label; /* C allows jumping past an initialization */
          ^
prog.cpp:53:13: error:   crosses initialization of ‘int x’
         int x = 0;
             ^
prog.cpp: In function ‘int main()’:
prog.cpp:66:8: error: too many arguments to function ‘void f()’
     f(1); /* doesn't match declaration in C++ */
        ^
prog.cpp:63:6: note: declared here
 void f();
      ^
prog.cpp:69:13: error: expected unqualified-id before ‘new’
         int new = 0;
             ^
prog.cpp:75:24: error: ‘puts’ was not declared in this scope
     puts("C is not C++");
                        ^
prog.cpp:80:12: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
         ip = vp; /* cast required in C++, not in C */
            ^
stdout
Standard output is empty