| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
10 Dec 2004 10:47:50 AM |
| Object: |
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
.
|
|
| User: "Jonathan Bartlett" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 11:32:18 AM |
|
|
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
.
|
|
|
| User: "Alex Vinokur" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 12:42:03 PM |
|
|
"Jonathan Bartlett" <johnnyb@eskimo.com> wrote in message news:41b9ebca$1@news.tulsaconnect.com...
jrefactors@hotmail.com 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
.
|
|
|
| User: "Jonathan Bartlett" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 03:29:24 PM |
|
|
* 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
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 04:53:51 PM |
|
|
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
.
|
|
|
|
|
| User: "Mark McIntyre" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 04:22:08 PM |
|
|
On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
<alexvn@big-foot.com> wrote:
"Jonathan Bartlett" <johnnyb@eskimo.com> 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>
.
|
|
|
| User: "Default User" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 05:20:33 PM |
|
|
Mark McIntyre wrote:
On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
<alexvn@big-foot.com> wrote:
"Jonathan Bartlett" <johnnyb@eskimo.com> 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
.
|
|
|
| User: "Mark McIntyre" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 10:11:59 AM |
|
|
On Fri, 10 Dec 2004 23:20:33 GMT, in comp.lang.c , "Default User"
<first.last@boeing.com.invalid> wrote:
Mark McIntyre wrote:
On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
<alexvn@big-foot.com> wrote:
"Jonathan Bartlett" <johnnyb@eskimo.com> 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?
This code
void foo(char a[2][2]) {
// do nothing
}
int main(void) {
char** a;
foo(a);
return 0;
}
will cause most C++ compilers to abort, as in C++ there's no possible
conversion from char** to *char[2].
A C compiler will typically complain but compile it anyway, perhaps since
there is often a practical way to perform the conversion.
This may simply be a QOI issue or a case of getting overchummy with the
implementation. But I've come across this in real live production code
written by 3rd parties of great eminence.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
.
|
|
|
| User: "Dik T. Winter" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 07:25:35 PM |
|
|
In article <7d6mr0h2qs3t2qtfhb85jlf92ecid1eb9h@4ax.com> Mark McIntyre <markmcintyre@spamcop.net> writes:
....
void foo(char a[2][2]) {
// do nothing
}
int main(void) {
char** a;
foo(a);
return 0;
}
will cause most C++ compilers to abort, as in C++ there's no possible
conversion from char** to *char[2].
A C compiler will typically complain but compile it anyway, perhaps since
there is often a practical way to perform the conversion.
There is *never* a practical way to perform the conversion. What is
happening is that you are invoking an implicit cast between two
incompatible pointer types. A C compiler will also compile:
void foo(double *a) {return; }
int main(void) { int *a; foo(a); return 0: }
which makes just as much sense.
In both cases using 'a' will in general lead to strange behaviour of the
program.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 09:41:10 PM |
|
|
In article <I8L5An.K36@cwi.nl>, Dik T. Winter <Dik.Winter@cwi.nl> wrote:
In article <7d6mr0h2qs3t2qtfhb85jlf92ecid1eb9h@4ax.com> Mark McIntyre <markmcintyre@spamcop.net> writes:
...
void foo(char a[2][2]) {
// do nothing
}
int main(void) {
char** a;
foo(a);
return 0;
}
will cause most C++ compilers to abort, as in C++ there's no possible
conversion from char** to *char[2].
A C compiler will typically complain but compile it anyway, perhaps since
there is often a practical way to perform the conversion.
There is *never* a practical way to perform the conversion. What is
happening is that you are invoking an implicit cast between two
incompatible pointer types. A C compiler will also compile:
void foo(double *a) {return; }
int main(void) { int *a; foo(a); return 0: }
which makes just as much sense.
In both cases using 'a' will in general lead to strange behaviour of the
program.
G:\tmp>como --c90 --A incompat.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C90
"incompat.cpp", line 2: error: argument of type "int *" is incompatible with
parameter of type "double *"
int main(void) { int *a; foo(a); return 0; }
^
"incompat.cpp", line 2: warning: variable "a" is used before its value is set
int main(void) { int *a; foo(a); return 0; }
^
1 error detected in the compilation of "incompat.cpp".
G:\tmp>como --c99 --A incompat.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C99
"incompat.cpp", line 2: error: argument of type "int *" is incompatible with
parameter of type "double *"
int main(void) { int *a; foo(a); return 0; }
^
"incompat.cpp", line 2: warning: variable "a" is used before its value is set
int main(void) { int *a; foo(a); return 0; }
^
1 error detected in the compilation of "incompat.cpp".
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
|
|
| User: "Joona I Palaste" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 12:20:05 PM |
|
|
Mark McIntyre <markmcintyre@spamcop.net> scribbled the following
on comp.lang.c:
On Fri, 10 Dec 2004 23:20:33 GMT, in comp.lang.c , "Default User"
<first.last@boeing.com.invalid> wrote:
Mark McIntyre wrote:
On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
<alexvn@big-foot.com> wrote:
"Jonathan Bartlett" <johnnyb@eskimo.com> 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?
This code
void foo(char a[2][2]) {
// do nothing
}
int main(void) {
char** a;
foo(a);
return 0;
}
will cause most C++ compilers to abort, as in C++ there's no possible
conversion from char** to *char[2].
A C compiler will typically complain but compile it anyway, perhaps since
there is often a practical way to perform the conversion.
This may simply be a QOI issue or a case of getting overchummy with the
implementation. But I've come across this in real live production code
written by 3rd parties of great eminence.
I don't understand. Although the C standard says that the type char[2]
is assignable to the type char *, it also says that the type char[2][2]
is definitely *not* assignable to the type char **.
When you indirect into a char[2][2] or a char(*)[2], you expect to find
two bytes of storage right there. However, when you indirect into a
char **, you expect to find a pointer, which then points to another
byte, which might or might not be storage.
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
.
|
|
|
| User: "Mark McIntyre" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 06:16:33 PM |
|
|
On 11 Dec 2004 18:20:05 GMT, in comp.lang.c , Joona I Palaste
<palaste@cc.helsinki.fi> wrote:
Mark McIntyre <markmcintyre@spamcop.net> scribbled the following
on comp.lang.c:
(snip pretty contrived example of difference between C and C+ compiler)
I don't understand. Although the C standard says that the type char[2]
is assignable to the type char *, it also says that the type char[2][2]
is definitely *not* assignable to the type char **.
Yeah, maybe. But no C compiler I've encountered will reject this code,
while every C++ one will. Like I said, QOI, or what? I belive you're right,
but the fact is, C code that compiles /and runs just fine on multiple
implementations/ won't even compile under C++.
When you indirect into a char[2][2] or a char(*)[2], you expect to find
two bytes of storage right there. However, when you indirect into a
char **, you expect to find a pointer, which then points to another
byte, which might or might not be storage.
Could be, Zaphod.
But remind me, does an array degenerate to a pointer under certain
circumstances, and is a pointer to x roughly the same as the address of a
block of type x? Or not? Or ....My heads hurt.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
.
|
|
|
| User: "Dik T. Winter" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 07:56:03 PM |
|
|
In article <l73nr09j0gsgksjpph8ut31hfelb5n8eg5@4ax.com> Mark McIntyre <markmcintyre@spamcop.net> writes:
Yeah, maybe. But no C compiler I've encountered will reject this code,
while every C++ one will. Like I said, QOI, or what? I belive you're right,
but the fact is, C code that compiles /and runs just fine on multiple
implementations/ won't even compile under C++.
The difference between C++ and C is that in C++ conversions between
incompatible pointer types are not allowed. In C they are allowed,
but may produce garbage. For instance, the following will not produce
what you think it should produce:
#include <stdio.h>
void foo(char a[1][1]) {
printf("%c\n", a[0][0]);
return;
}
int main(void) {
char c, *b, **a;
a = &b; b = &c; c = 'd';
foo(a);
return 0;
}
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
.
|
|
|
| User: "Peter Nilsson" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 10:53:24 PM |
|
|
Dik T. Winter wrote:
The difference between C++ and C is that in C++ conversions between
incompatible pointer types are not allowed. In C they are allowed,
but may produce garbage. For instance, the following will not
produce what you think it should produce:
#include <stdio.h>
void foo(char a[1][1]) {
printf("%c\n", a[0][0]);
return;
}
int main(void) {
char c, *b, **a;
a = &b; b = &c; c = 'd';
foo(a);
This line should produce a diagnostic for the simple assignment
constraint violation (C99 6.5.16.1p1.)
return 0;
}
--
Peter
.
|
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 09:45:08 PM |
|
|
In article <I8L6pF.Ay@cwi.nl>, Dik T. Winter <Dik.Winter@cwi.nl> wrote:
In article <l73nr09j0gsgksjpph8ut31hfelb5n8eg5@4ax.com> Mark McIntyre <markmcintyre@spamcop.net> writes:
Yeah, maybe. But no C compiler I've encountered will reject this code,
while every C++ one will. Like I said, QOI, or what? I belive you're right,
but the fact is, C code that compiles /and runs just fine on multiple
implementations/ won't even compile under C++.
The difference between C++ and C is that in C++ conversions between
incompatible pointer types are not allowed. In C they are allowed,
but may produce garbage. For instance, the following will not produce
what you think it should produce:
#include <stdio.h>
void foo(char a[1][1]) {
printf("%c\n", a[0][0]);
return;
}
int main(void) {
char c, *b, **a;
a = &b; b = &c; c = 'd';
foo(a);
return 0;
}
G:\tmp>como --c90 --A notallowed.c
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C90
"notallowed.c", line 11: error: argument of type "char **" is incompatible
with parameter of type "char (*)[1]"
foo(a);
^
1 error detected in the compilation of "notallowed.c".
G:\tmp>como --c99 --A notallowed.c
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C99
"notallowed.c", line 11: error: argument of type "char **" is incompatible
with parameter of type "char (*)[1]"
foo(a);
^
1 error detected in the compilation of "notallowed.c".
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
|
| User: "Mark McIntyre" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 04:42:43 AM |
|
|
On Sun, 12 Dec 2004 01:56:03 GMT, in comp.lang.c , "Dik T. Winter"
<Dik.Winter@cwi.nl> wrote:
The difference between C++ and C is that in C++ conversions between
incompatible pointer types are not allowed. In C they are allowed,
but may produce garbage.
precisely my point. You might have some apparently-working C code which
simply would not compile under C++
For instance, the following will not produce
what you think it should produce:
Sorry to be rude, but how do you know what I expect it to produce?
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
.
|
|
|
| User: "Dik T. Winter" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 06:51:20 PM |
|
|
In article <jt7or0lq9tdg7tad5s9qrq7u62d9bmn0kg@4ax.com> Mark McIntyre <markmcintyre@spamcop.net> writes:
On Sun, 12 Dec 2004 01:56:03 GMT, in comp.lang.c , "Dik T. Winter"
<Dik.Winter@cwi.nl> wrote:
The difference between C++ and C is that in C++ conversions between
incompatible pointer types are not allowed. In C they are allowed,
but may produce garbage.
precisely my point. You might have some apparently-working C code which
simply would not compile under C++
Nope. You said:
A C compiler will typically complain but compile it anyway, perhaps since
there is often a practical way to perform the conversion.
There is simply *no* practical way to perform the conversion. So compilers
simply do nothing (in this case), which works as long as you do not access
the thing as if it were a "pointer to pointer to char".
For instance, the following will not produce
what you think it should produce:
Sorry to be rude, but how do you know what I expect it to produce?
I have no idea. You started talking about a practical way to make the
conversion, so I thought you were thinking that there was a way to make
the converted pointer work as expected.
Now I am at a loss. What do you *mean* when you say "a practical way to
make the conversion"?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
.
|
|
|
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Any C code are valid C++ code? |
11 Dec 2004 06:35:13 PM |
|
|
In article <l73nr09j0gsgksjpph8ut31hfelb5n8eg5@4ax.com>,
Mark McIntyre <markmcintyre@spamcop.net> wrote:
On 11 Dec 2004 18:20:05 GMT, in comp.lang.c , Joona I Palaste
<palaste@cc.helsinki.fi> wrote:
Mark McIntyre <markmcintyre@spamcop.net> scribbled the following
on comp.lang.c:
(snip pretty contrived example of difference between C and C+ compiler)
I don't understand. Although the C standard says that the type char[2]
is assignable to the type char *, it also says that the type char[2][2]
is definitely *not* assignable to the type char **.
Yeah, maybe. But no C compiler I've encountered will reject this code,
while every C++ one will. Like I said, QOI, or what? I belive you're right,
but the fact is, C code that compiles /and runs just fine on multiple
implementations/ won't even compile under C++.
Note the MODEs lines:
G:\tmp>como fooa.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:non-strict warnings microsoft C++
"fooa.cpp", line 10: error: argument of type "char **" is incompatible with
parameter of type "char (*)[2]"
foo(a);
^
"fooa.cpp", line 10: warning: variable "a" is used before its value is set
foo(a);
^
1 error detected in the compilation of "fooa.cpp".
G:\tmp>como --A fooa.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C++
"fooa.cpp", line 10: error: argument of type "char **" is incompatible with
parameter of type "char (*)[2]"
foo(a);
^
"fooa.cpp", line 10: warning: variable "a" is used before its value is set
foo(a);
^
1 error detected in the compilation of "fooa.cpp".
"fooa.cpp", line 10: error: argument of type "char **" is incompatible with
parameter of type "char (*)[2]"
foo(a);
^
"fooa.cpp", line 10: warning: variable "a" is used before its value is set
foo(a);
^
1 error detected in the compilation of "fooa.cpp".
G:\tmp>como --c fooa.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:non-strict warnings microsoft C90
"fooa.cpp", line 10: warning: argument of type "char **" is incompatible with
parameter of type "char (*)[2]"
foo(a);
^
"fooa.cpp", line 10: warning: variable "a" is used before its value is set
foo(a);
^
G:\tmp>como --c --A fooa.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C90
"fooa.cpp", line 2: error: expected an expression
// do nothing
^
"fooa.cpp", line 5: warning: parsing restarts here after previous syntax error
}
^
"fooa.cpp", line 5: error: expected a ";"
}
^
"fooa.cpp", line 10: error: argument of type "char **" is incompatible with
parameter of type "char (*)[2]"
foo(a);
^
"fooa.cpp", line 10: warning: variable "a" is used before its value is set
foo(a);
^
3 errors detected in the compilation of "fooa.cpp".
G:\tmp>como --c99 fooa.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:non-strict warnings microsoft C99
"fooa.cpp", line 10: warning: argument of type "char **" is incompatible with
parameter of type "char (*)[2]"
foo(a);
^
"fooa.cpp", line 10: warning: variable "a" is used before its value is set
foo(a);
^
G:\tmp>como --c99 --A fooa.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C99
"fooa.cpp", line 10: error: argument of type "char **" is incompatible with
parameter of type "char (*)[2]"
foo(a);
^
"fooa.cpp", line 10: warning: variable "a" is used before its value is set
foo(a);
^
1 error detected in the compilation of "fooa.cpp".
I can't imagine any other C++ or C compiler acting differently in
their strict modes too, at least to not even put out a warning.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
| User: "Mark McIntyre" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 04:37:03 AM |
|
|
On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (Greg
Comeau) wrote:
(snip long and hard to interpret compiler output stuff)
I can't imagine any other C++ or C compiler acting differently in
their strict modes too, at least to not even put out a warning.
I don't disagree, and indeed thats not what I said.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 08:20:13 AM |
|
|
In article <7p7or01pif28te5a68s5m3hfiff8or6npa@4ax.com>,
Mark McIntyre <markmcintyre@spamcop.net> wrote:
On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (Greg
Comeau) wrote:
(snip long and hard to interpret compiler output stuff)
I can't imagine any other C++ or C compiler acting differently in
their strict modes too, at least to not even put out a warning.
I don't disagree, and indeed thats not what I said.
Oh, I agree it's not what you said. You offered your facts
and opinions, I just continued in the same vein.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
|
| User: "Charles Richmond" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 03:14:00 PM |
|
|
Mark McIntyre wrote:
On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (Greg
Comeau) wrote:
(snip long and hard to interpret compiler output stuff)
I can't imagine any other C++ or C compiler acting differently in
their strict modes too, at least to not even put out a warning.
I don't disagree, and indeed thats not what I said.
Did anyone post the following:
void cfunc()
{
/*--*//*--*/ printf("Hi C!!!\n"); /*--*//*--*/
}
In C, "Hi C!!!" would be printed. In C++, the line
should be a comment...
--
+-------------------------------
| Charles and Francis Richmond
| richmond at plano dot net
| Re-Defeat Bush!!!
+-------------------------------
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 02:22:18 PM |
|
|
In article <41BCB499.187B0C37@plano.net>,
Charles Richmond <richmond@nospam.plano.net> wrote:
Mark McIntyre wrote:
On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (Greg
Comeau) wrote:
(snip long and hard to interpret compiler output stuff)
I can't imagine any other C++ or C compiler acting differently in
their strict modes too, at least to not even put out a warning.
I don't disagree, and indeed thats not what I said.
Did anyone post the following:
void cfunc()
{
/*--*//*--*/ printf("Hi C!!!\n"); /*--*//*--*/
}
In C, "Hi C!!!" would be printed. In C++, the line
should be a comment...
Not so.
G:\tmp>como --A --c90 notaslslcomment.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C90
G:\tmp>aout
Hi C!!!
G:\tmp>como --A --c99 notaslslcomment.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C99
G:\tmp>aout
Hi C!!!
G:\tmp>como --A --c++ notaslslcomment.cpp
Comeau C/C++ 4.3.4.1 (Sep 19 2004 11:33:48) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:strict errors C++
G:\tmp>aout
Hi C!!!
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
| User: "Mark McIntyre" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 04:26:51 PM |
|
|
On 12 Dec 2004 15:22:18 -0500, in comp.lang.c , (Greg
Comeau) wrote:
In article <41BCB499.187B0C37@plano.net>,
Charles Richmond <richmond@nospam.plano.net> wrote:
void cfunc()
{
/*--*//*--*/ printf("Hi C!!!\n"); /*--*//*--*/
}
In C, "Hi C!!!" would be printed. In C++, the line
should be a comment...
Not so.
True, but there are valid examples of where comments could operate
differently in C and C++.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 07:21:00 PM |
|
|
In article <mbhpr0d8v72e64vt99h0its9hr0dhva5lp@4ax.com>,
Mark McIntyre <markmcintyre@spamcop.net> wrote:
On 12 Dec 2004 15:22:18 -0500, in comp.lang.c , (Greg
Comeau) wrote:
In article <41BCB499.187B0C37@plano.net>,
Charles Richmond <richmond@nospam.plano.net> wrote:
void cfunc()
{
/*--*//*--*/ printf("Hi C!!!\n"); /*--*//*--*/
}
In C, "Hi C!!!" would be printed. In C++, the line
should be a comment...
Not so.
True, but there are valid examples of where comments could operate
differently in C and C++.
Indeed. And yet, in Standard C++, the line above should not be
a comment, and furthermore ;) isn't.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
|
| User: "Keith Thompson" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 05:35:09 PM |
|
|
Mark McIntyre <markmcintyre@spamcop.net> writes:
On 12 Dec 2004 15:22:18 -0500, in comp.lang.c , (Greg
Comeau) wrote:
In article <41BCB499.187B0C37@plano.net>,
Charles Richmond <richmond@nospam.plano.net> wrote:
void cfunc()
{
/*--*//*--*/ printf("Hi C!!!\n"); /*--*//*--*/
}
In C, "Hi C!!!" would be printed. In C++, the line
should be a comment...
Not so.
True, but there are valid examples of where comments could operate
differently in C and C++.
You mean C90 and C++, right? As far as I know, comments behave
identically in C99 and C++.
--
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.
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 07:30:12 PM |
|
|
In article <lnmzwj9js7.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:
Mark McIntyre <markmcintyre@spamcop.net> writes:
On 12 Dec 2004 15:22:18 -0500, in comp.lang.c , (Greg
Comeau) wrote:
In article <41BCB499.187B0C37@plano.net>,
Charles Richmond <richmond@nospam.plano.net> wrote:
void cfunc()
{
/*--*//*--*/ printf("Hi C!!!\n"); /*--*//*--*/
}
In C, "Hi C!!!" would be printed. In C++, the line
should be a comment...
Not so.
True, but there are valid examples of where comments could operate
differently in C and C++.
You mean C90 and C++, right?
Don't know what he means, but since // is C++ and C99,
then comment differences would be between C90 and C99, and
C90 and C++ mainly in areas with multi line expressions
involving mixtures of "old" and "new" comment styles.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
|
| User: "Mark McIntyre" |
|
| Title: Re: Any C code are valid C++ code? |
14 Dec 2004 06:10:48 PM |
|
|
On Sun, 12 Dec 2004 23:35:09 GMT, in comp.lang.c , Keith Thompson
<kst-u@mib.org> wrote:
Mark McIntyre <markmcintyre@spamcop.net> writes:
True, but there are valid examples of where comments could operate
differently in C and C++.
You mean C90 and C++, right? As far as I know, comments behave
identically in C99 and C++.
Yes.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
.
|
|
|
|
|
|
|
| User: "Raymond Martineau" |
|
| Title: Re: Any C code are valid C++ code? |
12 Dec 2004 09:31:30 PM |
|
|
On Sun, 12 Dec 2004 13:14:00 -0800, Charles Richmond <richmond@plano.net>
wrote:
Mark McIntyre wrote:
On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (Greg
Comeau) wrote:
(snip long and hard to interpret compiler output stuff)
I can't imagine any other C++ or C compiler acting differently in
their strict modes too, at least to not even put out a warning.
I don't disagree, and indeed thats not what I said.
Did anyone post the following:
void cfunc()
{
/*--*//*--*/ printf("Hi C!!!\n"); /*--*//*--*/
}
In C, "Hi C!!!" would be printed. In C++, the line
should be a comment...
That's not the correct construct. When the "/*" comment delimiter is first
encountered, all text is read until the first "*/", including the end
delimiter. Unless there is some form of nested comments going on (which
is non-standard in either language), the "//" is not read at all.
The following function, however, demonstrates the lanugage differences:
int main()
{
printf("%d\n", 6 //*
+ 2); //**/ 2 );
}
Under C89, you get "3". Under C99 or C++, you get "8".
.
|
|
|
|
|
|
|
|
|
| User: "Dave Thompson" |
|
| Title: Re: Any C code are valid C++ code? |
20 Dec 2004 12:29:03 AM |
|
|
xx
On Sat, 11 Dec 2004 16:11:59 +0000, Mark McIntyre
<markmcintyre@spamcop.net> wrote:
C lets you blur the distinction between ** and *[ ] and [ ][ ]
rather more, especially in function calls.
void foo(char a[2][2]) {
// do nothing
}
int main(void) {
char** a;
foo(a);
return 0;
}
will cause most C++ compilers to abort, as in C++ there's no possible
conversion from char** to *char[2].
A C compiler will typically complain but compile it anyway, perhaps since
there is often a practical way to perform the conversion.
Not really. On most (flat-addressed) machines you can 'convert' one
(data) pointer type to any other but it's still pointing to the wrong
thing. If you never use the result, as in your example, _that_ almost
always 'works' -- or can be made to by a cast. In neither language
does it work in the sense of being useful for anything.
On average and especially in the past C compilers have been more
permissive about 'ignoring' errors -- or rather, C++ compilers have
mostly deliberately been more restrictive. But this is not inherent in
either language; it is due more to the attitudes of the (groups of)
people who developed them early, leading to shared expectations, then
traditions (cue Tevye) that might reasonably be called cultures.
This may simply be a QOI issue or a case of getting overchummy with the
implementation. But I've come across this in real live production code
written by 3rd parties of great eminence.
If _this_ code was "live" -- i.e. actually executed and the result(s)
used -- I have trouble believing that. Depending on which sides write
and read, it should produce grossly wrong results or crash or at
"best" clobber memory that will likely break something else.
Now, I've seen lots of bogus code in production programs either not
used at all (cancelled or mistaken) or once used but obsoleted by
changes elsewhere, that people haven't fixed or removed because it
"isn't broken". Which isn't necessarily wrong; in some environments
there is a high cost to re-testing/validating _any_ production change,
no matter how small or "obvious"; cost and risk in deploying; and even
risk of breaking workarounds or adaptations. If the code is wrong but
not actually causing problems it can be worth living with it.
- David.Thompson1 at worldnet.att.net
.
|
|
|
|
|
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 10:59:28 AM |
|
|
wrote:
Since C is a subset of C++ [...]
Wrong premise. Wrong conclusion. The answer to your subj is "no".
.
|
|
|
| User: "gianguz" |
|
| Title: Re: Any C code are valid C++ code? |
10 Dec 2004 12:02:47 PM |
|
|
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
.
|
|
|
|
|

|
Related Articles |
|
|