473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C++ compatible with C ?

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
Jul 22 '05 #1
21 1473
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;

Jul 22 '05 #2
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
Jul 22 '05 #3
In article <41***********************@news.wanadoo.fr>,
jjleto <jj****@laposte.net> 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 dj******@csclub.uwaterloo.ca
If his homework involves writing DOS programs, he probably needs all the
help he can get.
--Richard Heathfield in comp.lang.c
Jul 22 '05 #4

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

Jul 22 '05 #5
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
Jul 22 '05 #6
"jjleto" <jj****@laposte.net> wrote in message
news:41***********************@news.wanadoo.fr...
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/
Jul 22 '05 #7
Dave Vandervies wrote:
In article <41***********************@news.wanadoo.fr>,
jjleto <jj****@laposte.net> 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. '
Jul 22 '05 #8


Karthik Kumar wrote:
Dave Vandervies wrote:
In article <41***********************@news.wanadoo.fr>,
jjleto <jj****@laposte.net> 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

Jul 22 '05 #9
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.
Jul 22 '05 #10
jjleto <jj****@laposte.net> wrote in message news:<41***********************@news.wanadoo.fr>.. .
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++.
Jul 22 '05 #11
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.

Jul 22 '05 #12
In article <cm*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.de> 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 dj******@csclub.uwaterloo.ca
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
Jul 22 '05 #13
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.
Jul 22 '05 #14
Stewart Gordon wrote in news:cm**********@sun-cc204.lut.ac.uk 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/
Jul 22 '05 #15
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.
Jul 22 '05 #16
In article <Xn**********************************@130.133.1.4> ,
Rob Williscroft <rt*@freenet.co.uk> 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 dj******@csclub.uwaterloo.ca
I mean, probably still not good enough for Richard Heathfield, but
what can you do, really?
--Ben Pfaff in comp.lang.c
Jul 22 '05 #17
In article <41***********************@news.zen.co.uk>,
john blackburn <jo*********************@lintonhealy.co.uk> 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 dj******@csclub.uwaterloo.ca
I mean, probably still not good enough for Richard Heathfield, but
what can you do, really?
--Ben Pfaff in comp.lang.c
Jul 22 '05 #18
Dave Vandervies wrote in news:cm**********@rumours.uwaterloo.ca in
comp.lang.c++:
In article <Xn**********************************@130.133.1.4> ,
Rob Williscroft <rt*@freenet.co.uk> 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/
Jul 22 '05 #19
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.
Jul 22 '05 #20
Stewart Gordon wrote:
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.


It's not double booked. There are two spaces for identifiers:
the program space, and the implementation space. All the standard
identifiers are in the space reserved for the implementation.

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


What conflict?
Jul 22 '05 #21
Ron Natalie wrote:

<snip>
It's not double booked. There are two spaces for identifiers:
the program space, and the implementation space. All the standard
identifiers are in the space reserved for the implementation.


I.e. the standard is invading the implementation's space.
What's the point of reserving identifiers if it's done in a way that
such conflicts are bound to arise?


What conflict?


The one that you snipped from my last post.

Stewart.
Jul 22 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: R. Rajesh Jeba Anbiah | last post by:
Kinda OT. I haven't yet moved to PHP5. But, interested to know how many of you _really_ started using it or moved? Are you doing any compatible tweaks specifically for PHP 5 (forward compatible) or...
1
by: dkomo | last post by:
I wrote a VB 6.0 program to create an Access database file and fill it with data. I used the following DAO statement: Set db_AllEquity = ws.CreateDatabase(Database_Name, dbLangGeneral,...
5
by: Leif K-Brooks | last post by:
I need to do some simple image editing (pretty much just scaling) in a GPLed program. Unfortunatly, both of the Python libraries for image editing that I found (gdmodule and PIL) are under the...
0
by: Lee Gillie | last post by:
We have systems built upon shared libraries, which are maintained by a group of programmers. We install these to the GAC on production servers. For saftey, when a programmer uses one of these...
9
by: bert76 | last post by:
can anyone suggest a couple of websites providing *compatible* javascript? of course there is http://javascript.internet.com/ http://javascriptkit.com/ and the likes, but time and again you have...
11
by: Kristoffer Arfvidson | last post by:
HI! I have a question.... when designing asp.net pages in vs.net I always see that labels are transformed to <span tags... I stopped using span and div before because of its lack of compatibility...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
5
by: P | last post by:
I was wondering what sort of changes I need to make in order to make an application to be DEP compatible with on Windows XP x64 version 2003. I have compiled an open source C application...
2
by: gordon | last post by:
hi in my application a file selection by user returns a pathname string like F:/images/png/my.png or F:/docs/text/somedoc.txt ....etc. I can get the extension using...
1
by: Nathan Sokalski | last post by:
On Microsoft's page about IE8 and Defining Document Compatibility at: http://msdn.microsoft.com/en-us/library/cc288325(VS.85).aspx They give the following code to add to Web.config: <?xml...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.