tags:

views:

1155

answers:

8

The comment to this answer got me wondering. I've always thought that C was a proper subset of C++, that is, any valid C code is valid C++ code by extension. Am I wrong about that? Is it possible to write a valid C program that is not valid C++ code?

EDIT: This is really similar to, but not an exact duplicate of this question.

+24  A: 

In general, yes C code is considered C++ code.

But C is not a proper subset in a strict sense. There are a couple of exceptions.

Here are some valid things in C that are not valid in C++:

int *new;//<-- new is not a keyword in C
char *p = malloc(1024); //void * to char* without cast

There are more examples too, but you get the idea.

I previously wrote a more extensive answer in a similar question here.

Brian R. Bondy
Wow, two minutes! It's like you were just lurking around waiting on someone to ask that question. :)
Bill the Lizard
I've been refreshing non stop for 37 days, and finally you asked it ;)
Brian R. Bondy
Technically they compile together (as noted above). But writting C code in a C++ file (aka C with classes) does not mean it is C++. The styles for each language is just so radically different, moving from C to C++ is like learning a new language. Moving from C++ to C is just too painful.
Martin York
Of course, but he asked whether it's *valid* C++, not whether it's *good* C++ ;-)
Steve Jessop
+2  A: 
typedef struct {
     int a, b, c;
} st;

st s = {
    .a = 1,
    .b = 2,
};

This is valid C code which does not compile in most C++ compilers. It is not part of the C++ spec as far as I know. However, some C++ compilers are "liberal" with certain parts of the language and allow things they shouldn't, just as a lot miss out on a few nuances that are in the spec but almost never used.

SoapBox
I think the reason compilers will accept this is if you set the C99 compatibility flag on, or don't turn it off.
Robert Gould
+1  A: 

I think it may be more correct to say that ANSI C is a subset of C++. Not K&R C.

Paul Beckingham
Bill the Lizard
Jonathan Leffler
@Jonathan: Thanks. I did not know that.
Bill the Lizard
+1  A: 

See also Is it true that there is no need to learn C because C++ contains everything. There is a large common subset of C and C++; there are lots of extensions in C++ that are not in C; there are some bits in C that are not in C++ (mostly the bits you don't want to use); and there are some bits that are in C and C++ but are different.

Jonathan Leffler
+5  A: 

A couple of other things that are valid C but not C++:

int func();
func(0,0); //Error in C++, but not in C

Also don't underestimate the impact of C++ having more keywords:

int new;  //Obviously an error in C++
Jason Baker
I'm pretty sure your first example is wrong. Comeau rejects it. If you provide a parameter list, then you have to honor it. If you leave the parameter list blank, then you can pass any parameters you want.
Rob Kennedy
You're correct. I edited it.
Jason Baker
+5  A: 

One thing that sets the two apart and comes up in day to day development is linkage and function name mangling. A C function compiled with a C compiler is not accessible to C++ unless the prototype is marked up with extern "C".

cadabra
+7  A: 

A few more:

C allows recursive calls to main, C++ does not

char foo[3] = "abc" is legal C, not C++

sizeof('A') == sizeof(int) is true in C and false in C++

There are even more changes with C99

Edit: I found a post that lists the majority of the differences. http://c-faq.com/misc/cplusplus.nr.html

Summary of why C is not proper subset of C++:

1) auto conversion from void * to any object/incomplete type

2) new keywords (a number of these)

3) due to structs becoming scoped

4) due to struct tags becoming typedefs

5) due to prototypes being required

6) implicit int rules

7) Recursive call to main

8) Due to // comments

9) due to character literals being type char and not int etc.

FigBug
Interesting, I didn't know about all 3 of those. Do you have a reference for C++ not allowing recursive calls to main? g++ will only complain about it if I add the -pedantic flag.
Adam Rosenfield
I can't find a reference to it, I just remember this came up a lot in comp.lang.c when people came looking for help with C/C++, which caused the regulars to flip out and yell "They are separate languages!"
FigBug
Note that // is permitted in C99 - which is the current C standard.
Jonathan Leffler
+11  A: 

Also note that C99 adds several features which aren't permitted in C++ (or are only supported using vendor extensions), such as builtin _Complex and _Imaginary data types, variable-length arrays (arrays sized at runtime rather than compile time), flexible array members (arrays declared as the last member of a struct that may contain an unspecified number of elements), and more.

For the exhaustive list of incompatibilities between C and C++, including changes with C99, see http://david.tribble.com/text/cdiffs.htm.

Josh Kelley