C++ compatible with C ?

Discussion in 'C++' started by jjleto, Oct 29, 2004.

  1. jjleto

    jjleto Guest

    Can I assume that if a .c file compiles ok with gcc it will also compile
    ok with a .cc extension with g++ ?

    Any example where this does not work ?

    regards,
    jjleto
     
    jjleto, Oct 29, 2004
    #1
    1. Advertising

  2. jjleto

    Ron Natalie Guest

    jjleto wrote:
    > Can I assume that if a .c file compiles ok with gcc it will also compile
    > ok with a .cc extension with g++ ?


    Absolutely not.

    > Any example where this does not work ?
    >

    Sure, one obvious example is that there are a few keywords in C++ that
    aren't keywords in C.

    Compile something like:
    int class;
     
    Ron Natalie, Oct 29, 2004
    #2
    1. Advertising

  3. jjleto wrote:
    > Can I assume that if a .c file compiles ok with gcc it will also compile
    > ok with a .cc extension with g++ ?
    >
    > Any example where this does not work ?


    I do not remember at the moment how strict gcc handles comments, but
    there was this old example of
    #v+
    [...]
    int i;
    i = 8 //* Harhar */ 2
    ;
    [...]
    #v-

    >
    > regards,
    > jjleto


    Greeting,
    Johannes

    --
    #macro s(a,b)sphere{<a,b,20>.3pigment{color x}}#end light_source{-4*x 1}
    #macro q(a)(a*a)#end#local C=-1;#while(C<1)s(512*(11*C-9)/q(121*C*C-198*
    C+97)-4,4.22*(3-11*C)/(exp(C*11/4)+2.12)+11*C-8.5)s(4-.15*exp(3.3*C),12/
    5-11*C/2)s(4-16*(q(C*C)-C*C),4*C+7/2)s(2*C-6,7.5)#local C=C+.01;#end//JB
     
    Johannes Bretscher, Oct 29, 2004
    #3
  4. In article <4182a1e5$0$31200$>,
    jjleto <> wrote:
    >Can I assume that if a .c file compiles ok with gcc it will also compile
    >ok with a .cc extension with g++ ?


    No.

    >Any example where this does not work ?


    Obvious example:
    int old,new; /*Valid C, invalid C++*/

    More subtle example:
    --------
    #include <stdio.h>
    int main(void)
    {
    int i=42//**/
    +7;
    if(i==49)
    if(sizeof 'a' == 1)
    puts("C++ compiler or unusual byte size");
    else /* // comments and 'a' is int*/
    #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
    puts("C99 compiler");
    #else
    puts("broken C89 compiler");
    #endif
    else
    puts("C89 compiler");

    return 0;
    }
    --------
    dave@goofy:~/clc (0) $ gcc test.c
    dave@goofy:~/clc (0) $ ./a.out
    broken C89 compiler
    dave@goofy:~/clc (0) $ gcc -ansi test.c
    dave@goofy:~/clc (0) $ ./a.out
    C89 compiler
    dave@goofy:~/clc (0) $ gcc -std=c99 test.c
    dave@goofy:~/clc (0) $ ./a.out
    C99 compiler
    dave@goofy:~/clc (0) $ cp test.c test.cc
    dave@goofy:~/clc (0) $ gcc test.cc
    dave@goofy:~/clc (0) $ ./a.out
    C++ compiler or unusual byte size
    dave@goofy:~/clc (0) $
    --------


    dave

    --
    Dave Vandervies
    If his homework involves writing DOS programs, he probably needs all the
    help he can get.
    --Richard Heathfield in comp.lang.c
     
    Dave Vandervies, Oct 29, 2004
    #4
  5. jjleto

    Default User Guest

    jjleto wrote:
    > Can I assume that if a .c file compiles ok with gcc it will also

    compile
    > ok with a .cc extension with g++ ?
    >
    > Any example where this does not work ?



    Besides the others listed, there's the problem of void pointers. In C,
    pointers to void can be assigned to any object pointer without a cast.
    Not so in C++.



    Brian
     
    Default User, Oct 29, 2004
    #5
  6. jjleto wrote:

    > Can I assume that if a .c file compiles ok with gcc it will also compile
    > ok with a .cc extension with g++ ?
    >
    > Any example where this does not work ?



    C++, with few exceptions (=differences) retains C90 as a subset.
    Regarding C99 vs C++98, there are *many* differences (e.g a built in
    _Complex type).


    Even with C90 itself there were differences. Two examples:

    The implicit conversion of void * to any pointer type in C,

    char c[2]="ab";



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 29, 2004
    #6
  7. jjleto

    Cy Edmunds Guest

    Cy Edmunds, Oct 29, 2004
    #7
  8. Dave Vandervies wrote:
    > In article <4182a1e5$0$31200$>,
    > jjleto <> wrote:
    >
    >>Can I assume that if a .c file compiles ok with gcc it will also compile
    >>ok with a .cc extension with g++ ?

    >
    >
    > No.
    >
    >
    >>Any example where this does not work ?

    >
    >
    > Obvious example:
    > int old,new; /*Valid C, invalid C++*/
    >
    > More subtle example:
    > --------
    > #include <stdio.h>
    > int main(void)
    > {
    > int i=42//**/
    > +7;
    > if(i==49)
    > if(sizeof 'a' == 1)
    > puts("C++ compiler or unusual byte size");
    > else /* // comments and 'a' is int*/
    > #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L


    Just curious, is this macro specified by the C standard,
    or only restricted to GNU implementation.

    --
    Karthik. http://akktech.blogspot.com .
    ' Remove _nospamplz from my email to mail me. '
     
    Karthik Kumar, Oct 30, 2004
    #8
  9. Karthik Kumar wrote:

    > Dave Vandervies wrote:
    > > In article <4182a1e5$0$31200$>,
    > > jjleto <> wrote:
    > >
    > >>Can I assume that if a .c file compiles ok with gcc it will also compile
    > >>ok with a .cc extension with g++ ?

    > >
    > >
    > > No.
    > >
    > >
    > >>Any example where this does not work ?

    > >
    > >
    > > Obvious example:
    > > int old,new; /*Valid C, invalid C++*/
    > >
    > > More subtle example:
    > > --------
    > > #include <stdio.h>
    > > int main(void)
    > > {
    > > int i=42//**/
    > > +7;
    > > if(i==49)
    > > if(sizeof 'a' == 1)
    > > puts("C++ compiler or unusual byte size");
    > > else /* // comments and 'a' is int*/
    > > #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

    >
    > Just curious, is this macro specified by the C standard,
    > or only restricted to GNU implementation.
    >


    it is specified by the standard.

    David
     
    David Lindauer, Oct 30, 2004
    #9
  10. jjleto wrote:

    > Can I assume that if a .c file compiles ok with gcc it will also compile
    > ok with a .cc extension with g++ ?
    >
    > Any example where this does not work ?
    >
    > regards,
    > jjleto



    I think, in practical terms, yes; as other posts point out, you may have the
    occasional small issue to fix.

    Interestingly I tried to compile something I wrote in Borland C++ with g++
    and had a couple of issues with that even; despite my attempts to avoid
    using anything other than absolute standard C++ issue 3. Different
    compilers can be fussier about certain syntactical issues than others.
     
    john blackburn, Oct 30, 2004
    #10
  11. jjleto

    Arijit Guest

    jjleto <> wrote in message news:<4182a1e5$0$31200$>...
    > Can I assume that if a .c file compiles ok with gcc it will also compile
    > ok with a .cc extension with g++ ?
    >
    > Any example where this does not work ?
    >
    > regards,
    > jjleto


    C99 has a few things that C++ doesn't. For example, Variable Length Arrays.

    void foo(int m)
    {
    int x[m];
    }

    compiles in C, not C++.
     
    Arijit, Oct 30, 2004
    #11
  12. jjleto

    Rolf Magnus Guest

    Dave Vandervies wrote:

    > dave@goofy:~/clc (0) $ cp test.c test.cc
    > dave@goofy:~/clc (0) $ gcc test.cc
    > dave@goofy:~/clc (0) $ ./a.out
    > C++ compiler or unusual byte size
    > dave@goofy:~/clc (0) $
    > --------


    What exactly do you mean by "unusual byte size"? "Byte" is by definition the
    size of a char in C as well as in C++, i.e. sizeof(char) is always 1.
     
    Rolf Magnus, Oct 30, 2004
    #12
  13. In article <cm01ti$1oj$02$-online.com>,
    Rolf Magnus <> wrote:
    >Dave Vandervies wrote:
    >
    >> dave@goofy:~/clc (0) $ cp test.c test.cc
    >> dave@goofy:~/clc (0) $ gcc test.cc
    >> dave@goofy:~/clc (0) $ ./a.out
    >> C++ compiler or unusual byte size
    >> dave@goofy:~/clc (0) $
    >> --------

    >
    >What exactly do you mean by "unusual byte size"? "Byte" is by definition the
    >size of a char in C as well as in C++, i.e. sizeof(char) is always 1.


    It's referring to bit count; in C, character constants have type int,
    so the program attempts to identify C++ by checking sizeof 'a' (which is
    sizeof(char) in C++ and sizeof(int) in C). But if CHAR_BIT is not less
    than 16, sizeof(int) is allowed to be 1, and the program can't correctly
    identify that case.

    Calling this "unusual" is a modern-general-purpose-processor-centric
    assumption, but it's the best I could do with the time I was willing to
    put into it.


    dave

    --
    Dave Vandervies
    If you're serious about computing, get Knuth I, II and III. Just
    /showing/ them to a computer is sometimes enough...
    --Richard Heathfield in comp.lang.c
     
    Dave Vandervies, Oct 30, 2004
    #13
  14. David Lindauer wrote:
    <snip>
    >>> #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

    >>
    >> Just curious, is this macro specified by the C standard,
    >> or only restricted to GNU implementation.

    >
    > it is specified by the standard.


    Then why does it begin with __? I thought that was for
    implementation-specific features.

    Stewart.
     
    Stewart Gordon, Nov 1, 2004
    #14
  15. Stewart Gordon wrote in news:cm5hru$noq$ in
    comp.lang.c++:

    > David Lindauer wrote:
    > <snip>
    >>>> #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
    >>>
    >>> Just curious, is this macro specified by the C standard,
    >>> or only restricted to GNU implementation.

    >>
    >> it is specified by the standard.

    >
    > Then why does it begin with __? I thought that was for
    > implementation-specific features.
    >


    It is, *except* where the Standard has previously reserved the
    identifier.

    Also __STDC_VERSION__ is only defined in C99 not C++, C++ does
    define __STDC__ though (which it got from C89 AIUI).

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Nov 1, 2004
    #15
  16. jjleto

    Ron Natalie Guest

    Stewart Gordon wrote:

    >
    > Then why does it begin with __? I thought that was for
    > implementation-specific features.
    >

    You thought wrong. The __ is RESERVED for the implementation,
    that means YOU CAN'T USE THEM. It doesn't mean the standard
    doesn't define some symbols in the reserved space.
     
    Ron Natalie, Nov 1, 2004
    #16
  17. In article <Xns9594BC89167E9ukcoREMOVEfreenetrtw@130.133.1.4>,
    Rob Williscroft <> wrote:

    >Also __STDC_VERSION__ is only defined in C99 not C++, C++ does
    >define __STDC__ though (which it got from C89 AIUI).


    It was actually introduced in C95, a rather less severe update to C90
    than C99 was.
    I believe that it was introduced to avoid changing the value of 1 for
    __STDC__, so that programs that checked it for `equal to 1' and not for
    `defined and nonzero' wouldn't break.

    (Does C++ also define __STDC__ as 1? That seems an odd choice to me.)


    dave

    --
    Dave Vandervies
    I mean, probably still not good enough for Richard Heathfield, but
    what can you do, really?
    --Ben Pfaff in comp.lang.c
     
    Dave Vandervies, Nov 1, 2004
    #17
  18. In article <41835726$0$27544$>,
    john blackburn <> wrote:
    >jjleto wrote:
    >
    >> Can I assume that if a .c file compiles ok with gcc it will also compile
    >> ok with a .cc extension with g++ ?
    >>
    >> Any example where this does not work ?
    >>
    >> regards,
    >> jjleto

    >
    >
    >I think, in practical terms, yes; as other posts point out, you may have the
    >occasional small issue to fix.


    In practical terms, a nontrivial chunk of well-written C code will be
    unlikely to compile as C++ without modification. The conversion to
    something that a C++ compiler will do the right thing with should be
    fairly straightforward, since most of the constructs that are valid in
    both languages but have different meaning aren't considered `good code'
    in either (though even there I'm sure there are exceptions), but the
    resulting code won't be identical.

    Of course, trying to write in the common subset of the two languages is
    even worse, unless you have a Really Good Reason.


    dave

    --
    Dave Vandervies
    I mean, probably still not good enough for Richard Heathfield, but
    what can you do, really?
    --Ben Pfaff in comp.lang.c
     
    Dave Vandervies, Nov 1, 2004
    #18
  19. Dave Vandervies wrote in news:cm66l1$ip3$ in
    comp.lang.c++:

    > In article <Xns9594BC89167E9ukcoREMOVEfreenetrtw@130.133.1.4>,
    > Rob Williscroft <> wrote:
    >
    >>Also __STDC_VERSION__ is only defined in C99 not C++, C++ does
    >>define __STDC__ though (which it got from C89 AIUI).

    >
    > It was actually introduced in C95, a rather less severe update to C90
    > than C99 was.


    Thanks, I'll try to remeber that but I can't make any guarantees :).

    > I believe that it was introduced to avoid changing the value of 1 for
    > __STDC__, so that programs that checked it for `equal to 1' and not for
    > `defined and nonzero' wouldn't break.
    >
    > (Does C++ also define __STDC__ as 1? That seems an odd choice to me.)
    >


    16.9/1
    ....

    __STDC__ Whether __STDC__ is predefined and if so, what its value
    is, are implementation-defined.

    So it only really allows it to be defined :), its still a reserved
    identifier though, so:

    #if defined( __STDC__ )

    is portable.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Nov 1, 2004
    #19
  20. Ron Natalie wrote:
    > Stewart Gordon wrote:
    >
    >>
    >> Then why does it begin with __? I thought that was for
    >> implementation-specific features.
    >>

    > You thought wrong. The __ is RESERVED for the implementation,
    > that means YOU CAN'T USE THEM. It doesn't mean the standard
    > doesn't define some symbols in the reserved space.


    Oh, so the range of identifiers is double-booked. So an implementation
    can define something beginning with __, only for a later version of the
    standard to redefine it to mean something completely different and
    potentially break lots of legacy code.

    What's the point of reserving identifiers if it's done in a way that
    such conflicts are bound to arise?

    Stewart.
     
    Stewart Gordon, Nov 2, 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. Ravikanth[MVP]
    Replies:
    1
    Views:
    367
    Jackal
    Jul 22, 2003
  2. J Landeros
    Replies:
    1
    Views:
    367
    Victor Garcia Aprea [MVP]
    Oct 23, 2003
  3. Bucko
    Replies:
    1
    Views:
    383
    Steve C. Orr [MVP, MCSD]
    Nov 3, 2003
  4. Thomas
    Replies:
    1
    Views:
    401
    Shawn B.
    Dec 3, 2003
  5. pantagruel
    Replies:
    0
    Views:
    273
    pantagruel
    Feb 17, 2006
Loading...

Share This Page