Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ compatible with C ?

Reply
Thread Tools

C++ compatible with C ?

 
 
jjleto
Guest
Posts: n/a
 
      10-29-2004
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-29-2004
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;

 
Reply With Quote
 
 
 
 
Johannes Bretscher
Guest
Posts: n/a
 
      10-29-2004
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
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      10-29-2004
In article <4182a1e5$0$31200$(E-Mail Removed)>,
jjleto <(E-Mail Removed)> 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 http://www.velocityreviews.com/forums/(E-Mail Removed)
If his homework involves writing DOS programs, he probably needs all the
help he can get.
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-29-2004

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

 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      10-29-2004
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
 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      10-29-2004
"jjleto" <(E-Mail Removed)> wrote in message
news:4182a1e5$0$31200$(E-Mail Removed)...
> 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


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

--
Cy
http://home.rochester.rr.com/cyhome/


 
Reply With Quote
 
Karthik Kumar
Guest
Posts: n/a
 
      10-30-2004
Dave Vandervies wrote:
> In article <4182a1e5$0$31200$(E-Mail Removed)>,
> jjleto <(E-Mail Removed)> 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. '
 
Reply With Quote
 
David Lindauer
Guest
Posts: n/a
 
      10-30-2004


Karthik Kumar wrote:

> Dave Vandervies wrote:
> > In article <4182a1e5$0$31200$(E-Mail Removed)>,
> > jjleto <(E-Mail Removed)> 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



 
Reply With Quote
 
john blackburn
Guest
Posts: n/a
 
      10-30-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
making ie compatible code firefox compatible using Greasemonkey? pantagruel Javascript 0 02-17-2006 02:26 PM
Arctic Cooling NV Silencer 5 Rev.3 *7800 Series* Compatible Silverstrand Front Page News 0 10-17-2005 09:51 PM
USB Wireless Adapters compatible with Win CE 2.0 =?Utf-8?B?U0E0MDU=?= Wireless Networking 1 05-28-2005 01:17 PM
Windows XP Compatible Wireless Networking Kit =?Utf-8?B?QWRhbSBQLiBWYWxlcml1cw==?= Wireless Networking 0 08-19-2004 04:15 AM
Is WPS compatible with Smart client as defined in WISPr guidelines nsmurthy Wireless Networking 0 08-13-2004 10:23 AM



Advertisments