Handmade Hero»Forums»Code
zao
Lars Viklund
1 posts
#52
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?
Andrew Bromage
183 posts / 1 project
Research engineer, resident maths nerd (Erdős number 3).
#55
Actual differences between C and C++
I think this is the quickest way to tell if you're using a C compiler or a C++ compiler:

1
int class;
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
#64
Actual differences between C and C++
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
Mārtiņš Možeiko
2562 posts / 2 projects
#67
Actual differences between C and C++
sizeof('a') is 4 in C, but 1 in C++
ben
5 posts
#81
Actual differences between C and 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)
1 posts
Actual differences between C and C++
Edited by mgarp on
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;