Any C code are valid C++ code?

M

Mark McIntyre

On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (e-mail address removed) (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.
 
M

Mark McIntyre

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?
 
F

Flash Gordon

On Sat, 11 Dec 2004 18:02:20 -0800

But I think that
most good C programmers are good C++ programmers as well
and don't mind supporting customer's legitimate requests
for compatibility.

I have known many good C programmers who have never written a single C++
class, let alone a complete C++ program and have no knowledge of OOP.

If a customer only had access to a C++ compiler then it *might* be valid
to consider making a C program compatible with C++, but in general they
should use the type of compiler the program was designed for.
 
G

Greg Comeau

On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (e-mail address removed) (Greg
Comeau) wrote:
(snip long and hard to interpret compiler output stuff)


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.
 
R

robert.thorpe

I have converted C code to C++ in practice. It is not simple and there
are many problems caused by tiny differences. So, I would say
practically speaking C is not a subset of C++.

If the author of the code writes it to work as both C and C++ then this
is not a problem.
 
R

robert.thorpe

You are right, there are very good reasons for writing in a well
defined language.
Attempting to write something of significant size that works as both C
and C++ is not simple. It ought not to be attempted unless those doing
it know exactly what they are doing, and are prepared to live with
occasionally (or frequently) testing using both types of compiler.
 
G

Greg Comeau

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!!!
 
C

Charles Richmond

Mark said:
On 11 Dec 2004 19:35:13 -0500, in comp.lang.c , (e-mail address removed) (Greg
Comeau) wrote:
(snip long and hard to interpret compiler output stuff)


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!!!
+-------------------------------
 
K

Keith Thompson

Malcolm said:
The C++ folks could have made "@friend" or some similar illegal C construct
the keyword in their language, and allowed continued use of "friend" as an
identifier.

Yes, they could. I think Objective C took this approach.
Maybe this would have been a good idea.

I disagree. It would address the "problem" of C++ keywords that
aren't C keywords, but it wouldn't address any of the other
incompatibilities. C++ has good reasons for making character literals
have type char rather than int, and for disallowing implicit
conversions between void* and other pointer types, reasons that don't
apply to C. Marking the additional keywords doesn't address that.

I put "problem" in quotation marks because it really isn't a problem
in practice, as long as you decide which language you want to use and
use a compiler that handles that language.

Programming in C is easier than programming in the subset of C that's
consistent with C++, and few programmers have any good reason to do
the latter.

I almost think that the commonality of C and C++ does more harm than
good.
 
K

Keith Thompson

Mark McIntyre said:
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++.
 
D

Dik T. Winter

Dik T. Winter wrote: .... ....
If you are uncomfortable with maintaining compatibility with C++,
that's a perfectly legitimate reason for not doing so.
But I think that most good C programmers are good C++
programmers as well and don't mind supporting customer's
legitimate requests for compatibility.

Perhaps I am not a good C programmer. I have had only two contacts
with C++. The first time when I had to implement a threads library
for C++ on a Sparc. The second time when I had to make a C++
program working on both SGI and Solaris, which was impossible with
identical code (the compilers supported different versions of C++).
 
D

Dik T. Winter

Given that languages always evolve over time and add in new keyords, in some
sense it can never be said that a later version of any language is
a superset of an (or all) previous version(s) of that same language. This
is because some program written to the earlier spec may have used as an
identifier something which is now reserved to the implementation.

This is only true for languages where keywords are reserved identifiers
(as in C and Pascal). This is false for languages where keywords are
identifiers, but not reserved (as in Fortran), or were keywords are not
identifiers (Algol 60, Algol 68 (*)).
 
D

Dik T. Winter

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".
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"?
 
G

Greg Comeau

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.
 
G

Greg Comeau

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.
 
R

Ron House

Malcolm said:
Partly wrong.

It's completely wrong. C is not a subset of C++. It makes as much sense
to say that "3 == 2" is not _completely_ wrong because 3 is pretty close
to 2. Thinking like this results in errors.

"C is a subset of C++" is completely wrong.

"C is almost a subset of C++" might be correct if everyone understands
"almost" suitably.
 
R

Raymond Martineau

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".
 
P

Peter Nilsson

Dik said:
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.)
 
D

dandelion

I've had to write code that was to all intents and purposes C in C++,
because C++ was more in keeping with the image of the company.

That qualifies as the most absurd reason i've heard to date for using any
particular language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top