Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is C not a subset of C++? [closed]

Tags:

c++

c

People also ask

Why is C not a subset of C++?

In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++. However, C++ supports every programming technique supported by C95 (C90 plus an Amendment) and earlier.

Is R an open subset of C?

If you want to prove that R is open in C, you have to prove that for any x∈R, there exists ϵ>0 such that Bx(ϵ)⊆R, where Bx(ϵ) is a ball in C, meaning that Bx(ϵ)={z∣z∈C,|z−x|<ϵ}.

Is a subset closed?

A subset A is said to be a closed subset of X if it contains all its limit points. The subset X is a closed subset of itself.


If you compare C89 with C++ then here are a couple of things

No tentative definitions in C++

int n;
int n; // ill-formed: n already defined

int[] and int[N] not compatible (no compatible types in C++)

int a[1];
int (*ap)[] = &a; // ill-formed: a does not have type int[]

No K&R function definition style

int b(a) int a; { } // ill-formed: grammar error

Nested struct has class-scope in C++

struct A { struct B { int a; } b; int c; };
struct B b; // ill-formed: b has incomplete type (*not* A::B)

No default int

auto a; // ill-formed: type-specifier missing

C99 adds a whole lot of other cases

No special handling of declaration specifiers in array dimensions of parameters

// ill-formed: invalid syntax
void f(int p[static 100]) { }

No variable length arrays

// ill-formed: n is not a constant expression
int n = 1;
int an[n];

No flexible array member

// ill-formed: fam has incomplete type
struct A { int a; int fam[]; }; 

No restrict qualifier for helping aliasing analysis

// ill-formed: two names for one parameter?
void copy(int *restrict src, int *restrict dst);

In C, sizeof('a') is equal to sizeof(int).

In C++, sizeof('a') is equal to sizeof(char).


C++ has new keywords as well. The following is valid C code but won't compile under C++:

int class = 1;
int private = 2;
int public = 3;
int virtual = 4;

There are plenty of things. Just a simple example (it should be enough to prove C is not a proper subset of C++):

int* test = malloc(100 * sizeof(int));

should compile in C but not in C++.