Handmade Hero » Forums » Code » Actual differences between C and C++
zao
Lars Viklund
1 posts
#52 Actual differences between C and C++
4 years, 6 months ago

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?
Pseudonym
Andrew Bromage
184 posts / 1 project

Research engineer, resident maths nerd (Erdős number 3).

#55 Actual differences between C and C++
4 years, 6 months ago

I think this is the quickest way to tell if you're using a C compiler or a C++ compiler:

1
int class;

sub f{($f)[email protected]_;print"$f(q{$f});";}f(q{sub f{($f)[email protected]_;print"$f(q{$f});";}f});
cmuratori
Casey Muratori
818 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++
4 years, 6 months ago

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
mmozeiko
Mārtiņš Možeiko
1934 posts / 1 project
#67 Actual differences between C and C++
4 years, 6 months ago

sizeof('a') is 4 in C, but 1 in C++
nofoam
ben
5 posts
#81 Actual differences between C and C++
4 years, 6 months ago

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)
mgarp
1 posts
#226 Actual differences between C and C++
4 years, 6 months ago Edited by mgarp on Dec. 6, 2014, 3:29 p.m.

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;