fork(1) 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) { }
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
C is not C++