Some general questions about C and good practice

B

bjarne

Richard said:
KimmoA said:
In "The C++ Programming Language" (Special Edition), on the bottom of
page 13 (chapter 1, section 1.5), he (Bjarne) says:

"However, good C programs tend to be C++ programs. For example, every
program in [The C Programming Language book], is a C++ program."

This must be where I got it from. Sure... I misquoted him, but it's
been a while since I read it. You can't say that it's entirely false,
though!

I not only _can_ say that it's entirely false, I _do_ say that it's
entirely false, and if Bjarne really wrote that, more than a little
dishonest of him.

Richard

Careful who you accuse of dishonesty. Check the acknowledgement section
of K&R2.

I have as much right to talk about C as just about anyone else. I have
the experience (incl. up-to-date experience) with C, and I have made my
contribution to C (check your history: it was major).

-- Bjarne Stroustrup; http://www.research.att.com/~bs
 
J

jmcgill

Charlton said:
KimmoA said:
In "The C++ Programming Language" (Special Edition), on the bottom of
page 13 (chapter 1, section 1.5), he (Bjarne) says:

"However, good C programs tend to be C++ programs. For example, every
program in [The C Programming Language book], is a C++ program."

This must be where I got it from. Sure... I misquoted him, but it's
been a while since I read it. You can't say that it's entirely false,
though!
I not only _can_ say that it's entirely false, I _do_ say that it's
entirely false, and if Bjarne really wrote that, more than a little
dishonest of him.

Those words are in the book, where he says they are. They do not mean
what he wants them to mean.
It's also entirely possible that at the time he wrote that, it was
true -- C++ did evolve out of C, after all. It seems to me that
"dishonest" involves more intent than I think can be accurately
ascribed here.

All the examples in K&R2 will compile in a conforming C++ compiler.
That is *NOT* the same thing as a specification that C++ is a proper
superset of C, which is the implication that Stroustrup's words are
being twisted in order to support.


Follup-To: comp.lang.c++
 
D

Default User

jmcgill said:
Default said:
He also said, ". . . good C programs tend to be C++ programs. For
instance, every program in [K&R 2] is a C++ program."

You have an edition/page number for that?

Third Edition, chapter 1.6, pg. 13-14.




Brian
 
J

jmcgill

Default said:
jmcgill said:
Default said:
He also said, ". . . good C programs tend to be C++ programs. For
instance, every program in [K&R 2] is a C++ program."
You have an edition/page number for that?

Third Edition, chapter 1.6, pg. 13-14.

Are you aware that Kernighan and Ritchie used Bjarne's compiler to
validate all their examples? Of *course* they work.

Stroustrup is not saying that C++ is specified to be a proper superset
of C. Did I not quote the same thing from the same book as a
counterpoint to what KimmoA was claiming? That "any well-written C
is supposed to compile as a C++ program?"

I guess it depends on whether you interpret words like 'supposed to' as
being a specification of some kind.

C can be expected to compile on a C++ toolchain if it is written in such
a way that it does compile. That's all we can really take from
Stroustrup's and K&R's references to each other, because that's little
more than a happy coincidence or a cooked-up example.

Followup-To: comp.lang.c++
 
C

Charlton Wilbur

jmcgill said:
Follup-To: comp.lang.c++

I'll follow-up to where I want to follow up to, thanks much.
All the examples in K&R2 will compile in a conforming C++ compiler.
That is *NOT* the same thing as a specification that C++ is a proper
superset of C, which is the implication that Stroustrup's words are
being twisted in order to support.

The larger claim is that C++ is a proper superset of *well-written* C.
Well, here's a trivial counterexample. (Not nearly so trivial as
using 'new' or 'delete' as variable names, of course, but that's
hardly playing fair.)

#include <stdlib.h>

int main (void)
{
char *s = malloc (25);
free (s);

return 0;
}

That's idiomatic C -- in particular, the *lack* of cast on the result
of malloc() is considered good style because a cast there may hide
errors. (The magic number 25 is bad style, but you knew that.) It
fails to compile in C++ because C++ does not allow implicit casts of
void pointers.

So the only way to make the assertion that "well-written C is a proper
subset of C++" true is to redefine "well-written C" to include only
things that are also valid C++.

(I don't have K&R2 close at hand, and so can't check if any of the
examples use implicit casts of void pointers. That would make a handy
refutation of Stroustrup's statement.)

Charlton
 
K

Keith Thompson

Then C++ has failed fundamentally, because most well-written C programs
of any reasonable complexity do not compile as C++.

The major reason for this, I think, is that casting the result of
malloc() (or calloc() or realloc()) is not required (and strongly not
recommended) in C, but necessary in C++. If it were not for that, I
think that most well-written C *would* be valid C++. (I'm ignoring
things like <stdio.h> vs. <cstdio>, which were added to C++ relatively
late in its development.)

But apparently the fact that casting the result of malloc() in C is a
bad idea wasn't clearly understood in the early days of ANSI C. The
examples in K&R2 do cast the result of malloc(). And the K&R2 errata
list, <http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html>, says:

142(6.5, toward the end): The remark about casting the return
value of malloc ("the proper method is to declare ... then
explicitly coerce") needs to be rewritten. The example is correct
and works, but the advice is debatable in the context of the
1988-1989 ANSI/ISO standards. It's not necessary (given that
coercion of void * to ALMOSTANYTYPE * is automatic), and possibly
harmful if malloc, or a proxy for it, fails to be declared as
returning void *. The explicit cast can cover up an unintended
error. On the other hand, pre-ANSI, the cast was necessary, and it
is in C++ also.

At the time K&R2 was written, and at the time that Mr. Stroustrup made
his statement that well-written C tends to be valid C++ (I don't have
the exact wording), it was a reasonable statement. As both languages
have evolved, along with our understanding of them, it has become
somewhat less true.

C is, as always, *nearly*, but not quite, a subset of C++. It's still
reasonably straightforward to write code that's valid in both C and
C++ (write good C code, add casts for malloc() and friends, avoid
identifiers that are keywords in C++ but not in C, and a few other
things). But there is rarely a good reason to do so.
 
D

Default User

jmcgill said:
Default said:
jmcgill said:
Default User wrote:

He also said, ". . . good C programs tend to be C++ programs. For
instance, every program in [K&R 2] is a C++ program."
You have an edition/page number for that?

Third Edition, chapter 1.6, pg. 13-14.

Are you aware that Kernighan and Ritchie used Bjarne's compiler to
validate all their examples? Of course they work.

So? That doesn't make them C++ programs according to the ISO Standard
for that language. They aren't.
C can be expected to compile on a C++ toolchain if it is written in
such a way that it does compile.

Which the examples given weren't.

Followup-To: comp.lang.c++

No.




Brian
 
R

Richard Heathfield

Charlton Wilbur said:

(I don't have K&R2 close at hand, and so can't check if any of the
examples use implicit casts of void pointers. That would make a handy
refutation of Stroustrup's statement.)

"We used Bjarne Stroustrup's C++ translator extensively for local testing of
our programs, and Dave Kristol provided us with an ANSI C compiler for
final testing." - Brian W Kernighan and Dennis M Ritchie, in the preface to
K&R2.

Since the implicit conversion of void * to T * is illegal in C++, and since
K&R tested their code on a C++ compiler (heaven knows why, but there it
is), I think it unlikely that they rely on such conversions. See also the
errata for the K&R2 book, at:

http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
 
K

Keith Thompson

Default User said:
jmcgill said:
Default said:
jmcgill wrote:
Default User wrote:
He also said, ". . . good C programs tend to be C++ programs. For
instance, every program in [K&R 2] is a C++ program."
You have an edition/page number for that?

Third Edition, chapter 1.6, pg. 13-14.

Are you aware that Kernighan and Ritchie used Bjarne's compiler to
validate all their examples? Of course they work.

So? That doesn't make them C++ programs according to the ISO Standard
for that language. They aren't.

It's unlikely that they would be. K&R2 was first published in 1988;
the ISO C++ Standard wasn't published until 1998. The C++ language
changed considerably in those ten years.
Which the examples given weren't.

The examples in K&R2 are, as far as I know, valid C++ *as of the time
they were written*.
 
M

Mark McIntyre

Any C++ program is per
definition (of its creator, Bjarne Stroustrup) also a C program

If you're lucky, you will now get a response from Bjarne himself,
pointing out that he never said that.
(or was it the other way around, or vice versa?).
hmm

Anyway... it's backwards-compatible!

Ya reckon?

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

I have now looked it up (yes -- I just HAD to!). Instead of quoting all
you people have said, I'll just write a reply.

In "The C++ Programming Language" (Special Edition), on the bottom of
page 13 (chapter 1, section 1.5), he (Bjarne) says:

"However, good C programs tend to be C++ programs. For example, every
program in [The C Programming Language book], is a C++ program."

This must be where I got it from. Sure... I misquoted him, but it's
been a while since I read it. You can't say that it's entirely false,
though!

Well, its demonstrably false in 2006, some 18 years after the comment
was written. I can provide oodles of well written C that simply won't
compile on a C++ compiler. Just find a programme with memory
allocation.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

Default User

Keith said:
It's unlikely that they would be. K&R2 was first published in 1988;
the ISO C++ Standard wasn't published until 1998. The C++ language
changed considerably in those ten years.


The examples in K&R2 are, as far as I know, valid C++ *as of the time
they were written*.

That's a good question. What was "C++" at the time? Did C++ of the ARM
allow implicit int declarations? I don't know.

However, the book in question was based on C++ as it was being
standardized. In fact, TC++PL 3rd covers that fact that code such as:

main()
{
return 0;
}


Is NOT valid C++.

Now, my goal is certainly not to bash Dr. Stroustrup. In fact, he once
took time to personally answer an email I had about a question. He has
a fine book, but there's a statement in it (probably inherited from
previous editions) that is somewhat questionable, and which tends to
cause some confusion.


We've doubtlessly given this topic more electrons than it deserves, so
I'll shaddap about it. If others have more to say, I'll read with
interest but leave it at that.




Brian
 
B

bjarne

Default said:
That's a good question. What was "C++" at the time? Did C++ of the ARM
allow implicit int declarations? I don't know.

However, the book in question was based on C++ as it was being
standardized. In fact, TC++PL 3rd covers that fact that code such as:

main()
{
return 0;
}


Is NOT valid C++.

as it is not valid C99. It was valid ARM C++ just as it was valid C89.


Now, my goal is certainly not to bash Dr. Stroustrup. In fact, he once
took time to personally answer an email I had about a question. He has
a fine book, but there's a statement in it (probably inherited from
previous editions) that is somewhat questionable, and which tends to
cause some confusion.

I think you should do more homework before casting doubt about other
people's statements.

On the topic of C and C++, I can recommend:

http://www.research.att.com/~bs/bs_faq.html#difference
http://www.research.att.com/~bs/bs_faq.html#merge

and especially the papers referred to there.

-- Bjarne Stroustrup; http://www.research.att.com/~bs
 
M

Mark McIntyre

as it is not valid C99. It was valid ARM C++ just as it was valid C89.

Thats the point though - your comment in the book related to C and C++
over a decade ago. Its demonstrably incorrect today, but that doesn't
stop people who don't know either language well enough from quoting
you.

I would suggest that you need to qualify the remark in the next
edition of the book, as you have now done on your website.

I agree completely with the remarks you've made there, especially the
last paragraph of the first reference.

By the way, in my opinion it is somewhat misleading to say "Well
written C tends to be legal C++ also" and then in a footnote add that
you're talking only of an outdated version. Thats kinda like saying
"every english speaking country is part of the British Empire" and
then in a footnote saying "please note that the above paragraphs apply
to 1700."
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard Heathfield

Mark McIntyre said:

By the way, in my opinion it is somewhat misleading to say "Well
written C tends to be legal C++ also"

That's not misleading. It's simply false.
and then in a footnote add that
you're talking only of an outdated version. Thats kinda like saying
"every english speaking country is part of the British Empire"

That's not misleading. It's simply true. :)
 
M

Mark McIntyre

Mark McIntyre said:



That's not misleading. It's simply false.

Its not, given the full context and the footnote which I specifically
mentioned. I think you'd be wise to read threads properly.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard Bos

bjarne said:
Richard said:
KimmoA said:
In "The C++ Programming Language" (Special Edition), on the bottom of
page 13 (chapter 1, section 1.5), he (Bjarne) says:

"However, good C programs tend to be C++ programs. For example, every
program in [The C Programming Language book], is a C++ program."

This must be where I got it from. Sure... I misquoted him, but it's
been a while since I read it. You can't say that it's entirely false,
though!

I not only _can_ say that it's entirely false, I _do_ say that it's
entirely false, and if Bjarne really wrote that, more than a little
dishonest of him.

Careful who you accuse of dishonesty.

My apologies; I did not realise that KimmoA was quoting from an edition
of the book that is so ancient it is useless in a discussion like this.

KimmoA: please don't do that. What Mr. Stroustrup wrote back then may
well have been true for a primitive, pre-any-Standards version of C and
a ditto version of C++, but both languages have advanced since, and it
hasn't been true for over a decade and a half.
I have as much right to talk about C as just about anyone else. I have
the experience (incl. up-to-date experience) with C, and I have made my
contribution to C (check your history: it was major).

I don't intend to disparage your contributions to the history of C or
the current day of C++, but... /hic Rhodos, hic salta/. I try to judge
everybody by what they write and say now, not by their opinions from
fifteen or twenty years ago. I don't even always agree with _myself_
twenty years ago; why should anybody else be different?

Richard
 
R

Richard Heathfield

Mark McIntyre said:
Its not, given the full context and the footnote which I specifically
mentioned.

It may not have been false at the time it was written, but it is certainly
false now.

<snip>
 
K

KimmoA

Richard said:
My apologies; I did not realise that KimmoA was quoting from an edition
of the book that is so ancient it is useless in a discussion like this.

KimmoA: please don't do that. What Mr. Stroustrup wrote back then may
well have been true for a primitive, pre-any-Standards version of C and
a ditto version of C++, but both languages have advanced since, and it
hasn't been true for over a decade and a half.

The book says "Copyright 2000" and "Reprinted with corrections in May
2003".
 
M

Mark McIntyre

Mark McIntyre said:


It may not have been false at the time it was written, but it is certainly
false now.

Yes, which is what I said, and indeed made exceptionally clear in the
quote that you gratuitously snipped. Whats your point exactly? You're
behaving oddly.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top