C is not a subset of C++?

O

Oliver Dain

I've seen it asserted in a few places that C is not a proper subset of
C++, but I don't understand the assertion. What things are valid C
that aren't valid C++?

Thanks.
 
D

Dave Vandervies

I've seen it asserted in a few places that C is not a proper subset of
C++, but I don't understand the assertion. What things are valid C
that aren't valid C++?

As an example, this will compile and run (but do nothing interesting)
as C, but not as C++:
--------
/*'.h' form of C headers is bad form in C++ - use <cstdlib> instead*/
#include <stdlib.h>

/*Implicit int - Valid (but deprecated) C, not valid C++*/
main()
{
/*'new' as variable name - Valid C, not valid C++*/
int *new;

/*Implicit conversion from void * - Valid C, not valid C++*/
new=malloc(42*sizeof *new);

/*Implicit conversion *to* void * is valid in both languages*/
free(new);

/*Casting malloc() - bad code in both languages
(Hides bugs like failing to #include <stdlib.h> in C,
new should be used instead of malloc in C++)
*/
/*Character constants with type int - valid C, silent bug in C++
(unless sizeof(int)==sizeof(char))
*/
new=malloc(17*sizeof 'a');

free(new);

/*Falling off the end of main() - different semantics depending on language
C90 returns unspecified exit status, C99 and C++ treat it like 'return 0'
*/
}
--------

Plus there are some more esoteric differences like different handling
of '//' comments that are unlikely to show up in code not deliberately
written to demonstrate them.

Google for 'subset' in comp.lang.c for more than you ever wanted to know
about this.


Note that if you're willing to get a bit more hand-wavey, you can
legitimately claim that the expressive power of C is a subset of the
expressive power of C++ (though this is less true with C99 than with C90);
a C program can be made into a valid C++ program with the same semantics
without any major structural changes (though there will probably be a
bunch of code-level changes that are needed).


dave
 
R

Russell Hanneken

Oliver Dain said:
I've seen it asserted in a few places that C is not a proper subset of
C++, but I don't understand the assertion. What things are valid C
that aren't valid C++?

There's a web page devoted to answering that exact question:

http://david.tribble.com/text/cdiffs.htm

I've found it to be an excellent reference.

Regards,

Russell Hanneken
(e-mail address removed)
 
D

Default User

Dave said:
/*Implicit int - Valid (but deprecated) C, not valid C++*/
main()

Actually NOT valid C according to the latest standard. You should make
that clear here as you did in later points.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top