Actual differences between C and C++

It's often stated out in the world that "C is a subset of C++". This is not always accurate, and parts of this sneaks into the streams at times.

One of the differences I would like to highlight which can mask errors is the semantics of an empty parameter list in C and in C++.

1
2
3
4
5
6
7
// C++
void f1(void) // compatibility syntax with C, takes no argument
void f2() // function taking no arguments

// C
void f3(void) // function taking no arguments
void f4() // function taking any number of arguments


A reason this matters is that if you define a function in C with an empty parameter list, you can accidentally provide arguments when calling it and it will compile.

Any other favorite pet differences and caveats between the very related languages you've encountered and that you want to highlight?
I think this is the quickest way to tell if you're using a C compiler or a C++ compiler:

1
int class;
Although I'm not a spec wrangler myself, my understanding is there are definitely a number of these differences, especially if you're talking about C99 vs. C++. I don't keep a catalog of them, I'm afraid, so I'm not the one to list them, but there are definitely a number of gotchas. I think bracket initializer lists is another sticky wicket, IIRC...

- Casey
sizeof('a') is 4 in C, but 1 in C++
const's at file scope have external linkage by default in C99

In C++ they have internal linkage! (i.e. declaring them static is redundant)
void* is casted implicitly to any other pointer in C, in C++ you have to cast it explicitly.

1
2
3
4
5
6
7
// C
void* vptr;
int* iptr = vptr;

// C++
void* vptr;
int* iptr = (int*)vptr;

Edited by mgarp on