C is not a subset of C++?

Discussion in 'C++' started by Oliver Dain, Sep 1, 2003.

  1. Oliver Dain

    Oliver Dain Guest

    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.
    Oliver Dain, Sep 1, 2003
    #1
    1. Advertising

  2. Oliver Dain

    Oliver S. Guest

    Oliver S., Sep 1, 2003
    #2
    1. Advertising

  3. Oliver Dain

    Oliver Dain Guest

    Oliver S. wrote:

    >> 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.

    >
    > Read this: http://www.research.att.com/~bs/3rd_compat.pdf


    Thanks much. That was a great reference.
    Oliver Dain, Sep 1, 2003
    #3
  4. In article <eZO4b.5032$>,
    Oliver Dain <> wrote:
    >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

    --
    Dave Vandervies
    [Y]ou can write bad code that just barely works in both languages, or good
    code that works only in one language -- so pick one, and write good code in
    that language. --Chris Torek in comp.lang.c (crossposted to comp.lang.c++)
    Dave Vandervies, Sep 2, 2003
    #4
  5. "Oliver Dain" <> wrote in message
    news:eZO4b.5032$...
    > 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
    Russell Hanneken, Sep 3, 2003
    #5
  6. Oliver Dain

    Default User Guest

    Dave Vandervies wrote:

    > /*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
    Default User, Sep 3, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guoqi Zheng

    subset of data using dataview??

    Guoqi Zheng, Jan 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,005
    Miha Markic
    Jan 19, 2004
  2. Andy
    Replies:
    0
    Views:
    414
  3. news.microsoft.com

    validators subset

    news.microsoft.com, May 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    352
    Peter Blum
    May 27, 2004
  4. =?Utf-8?B?S2V2aW4gQmFua3M=?=

    Deploying a subset of an asp.Net app

    =?Utf-8?B?S2V2aW4gQmFua3M=?=, Oct 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    =?Utf-8?B?S2V2aW4gQmFua3M=?=
    Oct 21, 2004
  5. Sebastian Mach
    Replies:
    5
    Views:
    308
Loading...

Share This Page