Any C code are valid C++ code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jrefactors@hotmail.com

    Any C code are valid C++ code?

    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

  • Victor Bazarov

    #2
    Re: Any C code are valid C++ code?

    jrefactors@hotm ail.com wrote:[color=blue]
    > Since C is a subset of C++ [...][/color]

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

    Comment

    • gianguz

      #3
      Re: Any C code are valid C++ code?

      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

      Comment

      • Jonathan Bartlett

        #4
        Re: Any C code are valid C++ code?

        jrefactors@hotm ail.com wrote:[color=blue]
        > Since C is a subset of C++[/color]

        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 incompatibiliti es includes:

        * different linking mechanisms (this is workaroundable w/ extern C)
        * different interpretation of multidimensiona l 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

        Comment

        • Alex Vinokur

          #5
          Re: Any C code are valid C++ code?


          "Jonathan Bartlett" <johnnyb@eskimo .com> wrote in message news:41b9ebca$1 @news.tulsaconn ect.com...[color=blue]
          > jrefactors@hotm ail.com wrote:[color=green]
          > > Since C is a subset of C++[/color]
          >
          > 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 incompatibiliti es includes:[/color]
          [snip]
          [color=blue]
          > * different interpretation of multidimensiona l arrays[/color]
          What is the difference?

          [snip]

          --
          Alex Vinokur
          email: alex DOT vinokur AT gmail DOT com





          Comment

          • Chris Barts

            #6
            Re: Any C code are valid C++ code?

            jrefactors@hotm ail.com wrote:[color=blue]
            > Since C is a subset of C++,[/color]

            Wrong. A common notion that is completely wrong.
            [color=blue]
            > so any C code or C libraries (printf(), scanf(), etc...)
            > are valid C++ code.[/color]

            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.
            [color=blue]
            > Is that correct? so even though the whole program
            > is written in
            > C, but with .cpp extension, we still consider as C++ program?[/color]

            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.

            Comment

            • Chris Torek

              #7
              Re: Any C code are valid C++ code?

              In article <1102697270.754 151.199660@c13g 2000cwb.googleg roups.com>
              <jrefactors@hot mail.com> wrote:[color=blue]
              >Since C is a subset of C++, so any C code or C libraries (printf(),
              >scanf(), etc...) are valid C++ code. Is that correct?[/color]

              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(str uct 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.

              Comment

              • Mark McIntyre

                #8
                Re: Any C code are valid C++ code?

                On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
                <alexvn@big-foot.com> wrote:
                [color=blue]
                >"Jonathan Bartlett" <johnnyb@eskimo .com> wrote[color=green]
                >> * different interpretation of multidimensiona l arrays[/color][/color]
                [color=blue]
                >What is the difference?[/color]

                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 >

                Comment

                • Jonathan Bartlett

                  #9
                  Re: Any C code are valid C++ code?

                  [color=blue][color=green]
                  >> * different interpretation of multidimensiona l arrays[/color]
                  >
                  > What is the difference?
                  >[/color]

                  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

                  Comment

                  • jcoffin@taeus.com

                    #10
                    Re: Any C code are valid C++ code?

                    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.
                    [color=blue]
                    >From the opposite viewpoint, most reasonably-written C can be converted[/color]
                    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.

                    Comment

                    • Victor Bazarov

                      #11
                      Re: Any C code are valid C++ code?

                      Jonathan Bartlett wrote:[color=blue][color=green][color=darkred]
                      >>> * different interpretation of multidimensiona l arrays[/color]
                      >>
                      >>
                      >> What is the difference?
                      >>[/color]
                      >
                      > 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.[/color]

                      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 multidimensiona l array is an array of arrays.
                      It is also true that in both languages all elements of a multidimensiona l
                      array sit next to each other, making it what you call "a static block".

                      V

                      Comment

                      • E. Robert Tisdale

                        #12
                        Re: Any C code are valid C++ code?

                        Victor Bazarov wrote:
                        [color=blue]
                        > jrefactors@hotm ail.com wrote:
                        >[color=green]
                        >> Since C is a subset of C++ [...][/color]
                        >
                        > Wrong premise. Wrong conclusion. The answer to your subj is "no".[/color]

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

                        Comment

                        • Richard Tobin

                          #13
                          Re: Any C code are valid C++ code?

                          In article <cpd60q$bn1$2@n ntp1.jpl.nasa.g ov>,
                          E. Robert Tisdale <E.Robert.Tisda le@jpl.nasa.gov > wrote:
                          [color=blue]
                          >Practically speaking, C is a subset of C++.[/color]

                          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(__cplus plus) to
                          allow it to be used in C++ programs without breaking backward
                          compatibility for existing (C) users.

                          -- Richard

                          Comment

                          • Default User

                            #14
                            Re: Any C code are valid C++ code?

                            Mark McIntyre wrote:
                            [color=blue]
                            > On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
                            > <alexvn@big-foot.com> wrote:
                            >[color=green]
                            > >"Jonathan Bartlett" <johnnyb@eskimo .com> wrote[color=darkred]
                            > >> * different interpretation of multidimensiona l arrays[/color][/color]
                            >[color=green]
                            > > What is the difference?[/color]
                            >
                            > C lets you blur the distinction between ** and *[ ] and [ ][ ]
                            > rather more, especially in function calls.[/color]

                            Eh, what? Could you explain?




                            Brian

                            Comment

                            • Keith Thompson

                              #15
                              Re: Any C code are valid C++ code?

                              "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:[color=blue]
                              > Victor Bazarov wrote:
                              >[color=green]
                              >> jrefactors@hotm ail.com wrote:
                              >>[color=darkred]
                              >>> Since C is a subset of C++ [...][/color]
                              >> Wrong premise. Wrong conclusion. The answer to your subj is "no".[/color]
                              >
                              > Practically speaking, C is a subset of C++.
                              > There are few exceptions.
                              > Each new C++ standard attempts to subsume each new C standard.[/color]

                              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_Keit h) kst-u@mib.org <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.

                              Comment

                              Working...