Any C code are valid C++ code?

Discussion in 'C++' started by jrefactors@hotmail.com, Dec 10, 2004.

  1. Guest

    Since C is a subset of C++, so any C code or C libraries (printf(),
    scanf(), etc...)
    are valid C++ code. Is that correct? so even though the whole program
    is written in
    C, but with .cpp extension, we still consider as C++ program?
    Please advise. Thanks
     
    , Dec 10, 2004
    #1
    1. Advertising

  2. wrote:
    > Since C is a subset of C++ [...]


    Wrong premise. Wrong conclusion. The answer to your subj is "no".
     
    Victor Bazarov, Dec 10, 2004
    #2
    1. Advertising

  3. wrote:
    > Since C is a subset of C++


    C is not a subset of C++. C++ has some incompatible changes from C.
    However, they are compatible enough that a lot of code runs in both.

    Some incompatibilities includes:

    * different linking mechanisms (this is workaroundable w/ extern C)
    * different interpretation of multidimensional arrays
    * many C programs include typedefs and define which override C++
    keywords, and therefore aren't allowed in C++ (typedef int bool; for
    example)

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
     
    Jonathan Bartlett, Dec 10, 2004
    #3
  4. gianguz Guest

    The answer is 'no' in general.For example one of the basic difference
    (even if you create a simple hello world program and compile it under
    with a C and a C++ compiler) between C and C++ relies almost in the
    name mangling mechanism. C++ uses an extended decoration method to give
    the linker indications about the name resolutions.
    That tecnique is not compatible with C declaration naming.
    This is the reason why you have to specify extern "C" {... } around C
    code to ensure
    compatibility.
    Obviously there are other things that makes the two language very far
    even so similar! ;)

    Gianguglielmo
     
    gianguz, Dec 10, 2004
    #4
  5. Alex Vinokur Guest

    "Jonathan Bartlett" <> wrote in message news:41b9ebca$...
    > wrote:
    > > Since C is a subset of C++

    >
    > C is not a subset of C++. C++ has some incompatible changes from C.
    > However, they are compatible enough that a lot of code runs in both.
    >
    > Some incompatibilities includes:

    [snip]

    > * different interpretation of multidimensional arrays

    What is the difference?

    [snip]

    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Dec 10, 2004
    #5
  6. Chris Barts Guest

    wrote:
    > Since C is a subset of C++,


    Wrong. A common notion that is completely wrong.

    > so any C code or C libraries (printf(), scanf(), etc...)
    > are valid C++ code.


    Not true. For example, the following valid C program is not valid C++:

    #include <stdlib.h>

    int main(void)
    {
    /* new is a reserved word in C++ */
    char new, *buf;
    /* Implicit conversion from void* to char* not valid in C++ */
    buf = malloc(1024);
    free(buf);
    return 0;
    }

    It is the case that you can use the C standard library functions in C++
    code. However, it is rarely the best way to accomplish the task.

    > Is that correct? so even though the whole program
    > is written in
    > C, but with .cpp extension, we still consider as C++ program?


    You can consider it a C++ program when it conforms to the relevant
    standards. Writing in C is not a good way to conform to those standards.
     
    Chris Barts, Dec 10, 2004
    #6
  7. Chris Torek Guest

    In article <>
    <> wrote:
    >Since C is a subset of C++, so any C code or C libraries (printf(),
    >scanf(), etc...) are valid C++ code. Is that correct?


    No.

    Compile the following program as a C program and run it. Then,
    compile it as a C++ program and run that. Observe the different
    output.

    #include <stdio.h>

    struct A { char c[1000]; };

    int main(void) {
    struct B { struct A { char c[1]; } a; char c[1]; };

    printf("sizeof(struct A): %lu\n", (unsigned long)sizeof(struct A));
    return 0;
    }

    This is, of course, a carefully-constructed example -- but real C
    programs really do fail to work when compiled as C++ programs,
    sometimes, because of small but significant semantic changes.

    (Exercise: *why* is the output different?)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Dec 10, 2004
    #7

  8. >> * different interpretation of multidimensional arrays

    >
    > What is the difference?
    >


    Apparently I was incorrect. I had thought that C allocated them in a
    static block while C++ allocated them as arrays of arrays, but a little
    experimentation showed my ideas to be faulty.

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
     
    Jonathan Bartlett, Dec 10, 2004
    #8
  9. Victor Bazarov wrote:

    > wrote:
    >
    >> Since C is a subset of C++ [...]

    >
    > Wrong premise. Wrong conclusion. The answer to your subj is "no".


    Practically speaking, C is a subset of C++.
    There are few exceptions.
    Each new C++ standard attempts to subsume each new C standard.
     
    E. Robert Tisdale, Dec 10, 2004
    #9
  10. On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
    <> wrote:

    >"Jonathan Bartlett" <> wrote
    >> * different interpretation of multidimensional arrays


    >What is the difference?


    C lets you blur the distinction between ** and *[ ] and [ ][ ] rather
    more, especially in function calls.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Dec 10, 2004
    #10
  11. Guest

    C is not a subset of C++.

    The C++ standard does require support for the C library, but with a few
    changes in how that support must be provided (e.g. more restrictions on
    which parts can be implemented as macros).

    There are other differences, some of which have been pointed out
    already, but I'll add yet another bit of code to demonstrate a
    difference I haven't seen pointed out yet:

    char x[sizeof('a')-1];

    All properly-functioning C++ compilers are guaranteed to reject this.
    In theory a C compiler could reject it as well, but I've never seen one
    that did.

    >From the opposite viewpoint, most reasonably-written C can be converted

    to C++ with only minimal changes, perhaps the most obvious of which is
    that in well-written C, there should not be an explicit cast on the
    value returned by malloc, while C++ requires one -- though well-written
    C++ will rarely use malloc at all.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.


    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    , Dec 10, 2004
    #11
  12. Jonathan Bartlett wrote:
    >>> * different interpretation of multidimensional arrays

    >>
    >>
    >> What is the difference?
    >>

    >
    > Apparently I was incorrect. I had thought that C allocated them in a
    > static block while C++ allocated them as arrays of arrays, but a little
    > experimentation showed my ideas to be faulty.


    No, your ideas are correct. The incorrect part is that you think that
    there is a difference between "static block" and "array of arrays".

    In fact, in both languages a multidimensional array is an array of arrays.
    It is also true that in both languages all elements of a multidimensional
    array sit next to each other, making it what you call "a static block".

    V
     
    Victor Bazarov, Dec 10, 2004
    #12
  13. In article <cpd60q$bn1$>,
    E. Robert Tisdale <> wrote:

    >Practically speaking, C is a subset of C++.


    Practically speaking, I've found that's not the case. On several
    occasions, people have mailed me to ask why one of my C programs
    doesn't compile, and the answer has turned out to be that they were
    trying to compile it as C++.

    I have also had to conditionalize a .h file on defined(__cplusplus) to
    allow it to be used in C++ programs without breaking backward
    compatibility for existing (C) users.

    -- Richard
     
    Richard Tobin, Dec 10, 2004
    #13
  14. Default User Guest

    Mark McIntyre wrote:

    > On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
    > <> wrote:
    >
    > >"Jonathan Bartlett" <> wrote
    > >> * different interpretation of multidimensional arrays

    >
    > > What is the difference?

    >
    > C lets you blur the distinction between ** and *[ ] and [ ][ ]
    > rather more, especially in function calls.


    Eh, what? Could you explain?




    Brian
     
    Default User, Dec 10, 2004
    #14
  15. Richard Tobin wrote:

    > E. Robert Tisdale wrote:
    >
    >>Practically speaking, C is a subset of C++.

    >
    > Practically speaking, I've found that's not the case.
    > On several occasions, people have mailed me to ask
    > why one of my C programs doesn't compile,
    > and the answer has turned out to be that
    > they were trying to compile it as C++.
    >
    > I have also had to conditionalize a .h file on defined(__cplusplus)
    > to allow it to be used in C++ programs
    > without breaking backward compatibility for existing (C) users.


    From which I conclude that
    it isn't very difficult at all for you
    to ensure that your C code will compile as C++ code
    and perform as expected.
     
    E. Robert Tisdale, Dec 10, 2004
    #15
  16. "E. Robert Tisdale" <> writes:
    > Victor Bazarov wrote:
    >
    >> wrote:
    >>
    >>> Since C is a subset of C++ [...]

    >> Wrong premise. Wrong conclusion. The answer to your subj is "no".

    >
    > Practically speaking, C is a subset of C++.
    > There are few exceptions.
    > Each new C++ standard attempts to subsume each new C standard.


    The number of C constructs that either are invalid C++ or are valid
    C++ with different semantics is small.

    The number of real-world well-written C programs that use those
    constructs is much larger.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 10, 2004
    #16
  17. In article <cpdbl0$f44$>,
    E. Robert Tisdale <> wrote:
    > From which I conclude that
    >it isn't very difficult at all for you
    >to ensure that your C code will compile as C++ code
    >and perform as expected.


    Possibly not, but I have no incentive to do so.

    -- Richard
     
    Richard Tobin, Dec 11, 2004
    #17
  18. Richard Tobin wrote:

    > E. Robert Tisdale wrote:
    >
    >>From which I conclude that
    >>it isn't very difficult at all for you
    >>to ensure that your C code will compile as C++ code
    >>and perform as expected.

    >
    > Possibly not, but I have no incentive to do so.


    Evidently, you don't think of the people who
    "have mailed me to ask why one of my C programs doesn't compile"
    as customers or employers.
     
    E. Robert Tisdale, Dec 11, 2004
    #18
  19. In article <cpdj5o$ipi$> writes:
    > Richard Tobin wrote:
    > > E. Robert Tisdale wrote:
    > >>From which I conclude that
    > >>it isn't very difficult at all for you
    > >>to ensure that your C code will compile as C++ code
    > >>and perform as expected.

    > >
    > > Possibly not, but I have no incentive to do so.

    >
    > Evidently, you don't think of the people who
    > "have mailed me to ask why one of my C programs doesn't compile"
    > as customers or employers.


    Probably you have not thought that the C programs involved possibly
    were freeware? Obviously, if the person complaining is a customer
    or an employer, there is pretty good incentive to make changes.
    When somebody complains about a program I put out for free, I will
    simply let it go if it is some incompatibility. Only when a (good)
    suggestion for change is involved am I inclined to modify. Why
    should I put in my free time in changing things that work for me as
    I want?
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Dec 11, 2004
    #19
  20. "Dik T. Winter" <> wrote...
    > [...] Why
    > should I put in my free time in changing things that work for me as
    > I want?


    Why did you feel compelled to publish it in the first place? Can
    the same reason apply to compel you to fix it? If not, was it the
    _real_ reason you published your code? You don't have to answer.

    V
     
    Victor Bazarov, Dec 11, 2004
    #20
    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. Replies:
    2
    Views:
    452
    Adam P. Jenkins
    Jan 18, 2005
  2. Eric Biller

    Embedding VRML-Objects in valid code

    Eric Biller, Nov 13, 2003, in forum: HTML
    Replies:
    6
    Views:
    3,798
    Toby A Inkster
    Nov 14, 2003
  3. Replies:
    5
    Views:
    562
    Stan Kitsis [MSFT]
    Jan 20, 2005
  4. Gianni Mariani

    is this code valid ?

    Gianni Mariani, May 17, 2004, in forum: C++
    Replies:
    5
    Views:
    370
    Gianni Mariani
    May 19, 2004
  5. Any C code are valid C++ code?

    , Dec 10, 2004, in forum: C Programming
    Replies:
    67
    Views:
    1,190
    Dave Thompson
    Dec 20, 2004
Loading...

Share This Page