/* A bunch of code that compiles and runs under C89 but fails under any C++
*
*
* Ryan Haining
* Source may be redistributed freely for educational purposes as long as it
* contains this notice.
* If you're redistributing for commerical purposes, err, you may want to
* rethink that decision.
*/
/* type aliases and struct names occupy separate namespaces in C, not in C++ */
struct S { int i; };
typedef int S;
struct Outer { struct Inner { int i; } in; };
/* struct Inner will be Outer::Inner in C++ due to name scope */
struct Inner inner;
/* default return type of int in C, C++ functions need explicit return types */
g() {
return 0;
}
/* C sees this as two declarations of the same integer,
* C++ sees it as redefinition */
int n;
int n;
/* K&R style argument type declarations */
void h(i) int i; { }
/* struct type declaration in return type */
struct S2{int a;} j(void) { struct S2 s = {1}; return s; }
/* struct type declaration in argument, stupid and useless, but valid */
/*void dumb(struct S3{int a;} s) { } */
/* enum/int assignment */
enum E{A, B};
enum E e = 1;
void k() {
goto label; /* C allows jumping past an initialization */
{
int x = 0;
label:
x = 1;
}
}
/* () in declaration means unspecified number of arguments in C, the definition
* can take any number of arguments,
* but means the same as (void) in C++ (definition below main) */
void f();
int main (void) {
f(1); /* doesn't match declaration in C++ */
{
/* new is a keyword in C++ */
int new = 0;
}
/* no stdio.h include results in implicit definiton in C. However,
* as long as a matching function is found at link-time, it's fine.
* C++ requires a declaration for all called functions */
{
int *ip;
void *vp = 0;
ip = vp; /* cast required in C++, not in C */
}
return 0;
}
/* matches declaration in C, not in C++ */
void f(int i) { }
LyogQSBidW5jaCBvZiBjb2RlIHRoYXQgY29tcGlsZXMgYW5kIHJ1bnMgdW5kZXIgQzg5IGJ1dCBmYWlscyB1bmRlciBhbnkgQysrIAogKgogKgogKiBSeWFuIEhhaW5pbmcKICogU291cmNlIG1heSBiZSByZWRpc3RyaWJ1dGVkIGZyZWVseSBmb3IgZWR1Y2F0aW9uYWwgcHVycG9zZXMgYXMgbG9uZyBhcyBpdAogKiBjb250YWlucyB0aGlzIG5vdGljZS4KICogSWYgeW91J3JlIHJlZGlzdHJpYnV0aW5nIGZvciBjb21tZXJpY2FsIHB1cnBvc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