468,463 Members | 2,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

Some general questions about C and good practice

Hey! Some questions about C that have been bugging me for a while...

1) Is inline a valid C keyword or not? I was kind of surprised of not
finding it in C, to be honest. My "The C Programming Language" book
doesn't mention it.

2) I understand that C doesn't care about whitespace that much, but why
did they make it impossible to use the minus ('-') char in variable
names? I now have to incorrectly name my "hi-score" variable "hiscore".
:(

3) Is using "const" in C considered good practice? I have had little
need to do so so far thanks to #define.

4) I kind of hate C-like comments. I use C++ comments mostly in my C
programs. Is this evil?

5) I feel kind of naked without bool, but I understand that by the way
computers work, an int holding only 0 or 1 makes the most sense. Or
something. Does it?

PS: I started with C++ and went "back" to C after several years of not
getting anything done thanks to the damn OOP. In C, I'm all creative
again! ^__^

--
http://www.kimmoa.se/

Sep 11 '06
66 3252
On 12 Sep 2006 19:40:17 -0700, in comp.lang.c , "KimmoA"
<ki****@gmail.comwrote:
>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
Sep 13 '06 #51
Keith Thompson wrote:
"Default User" <de***********@yahoo.comwrites:
jmcgill wrote:
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.
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.

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

Sep 13 '06 #52

Default User wrote:
Keith Thompson wrote:
"Default User" <de***********@yahoo.comwrites:
jmcgill wrote:
>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.
>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.
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++.
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

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
Sep 14 '06 #53
On 13 Sep 2006 17:25:37 -0700, in comp.lang.c , "bjarne"
<bj****@gmail.comwrote:
>
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.
>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
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
Sep 14 '06 #54
Mark McIntyre said:

<snip>
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. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 14 '06 #55
On Thu, 14 Sep 2006 10:25:27 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>Mark McIntyre said:

<snip>
>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.
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
Sep 14 '06 #56
"bjarne" <bj****@gmail.comwrote:
Richard Bos wrote:
"KimmoA" <ki****@gmail.comwrote:
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
Sep 14 '06 #57
Mark McIntyre said:
On Thu, 14 Sep 2006 10:25:27 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>>Mark McIntyre said:

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

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>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 14 '06 #58
Richard Bos wrote:
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".

--
http://www.kimmoa.se/

Sep 14 '06 #59
On Thu, 14 Sep 2006 12:13:56 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>Mark McIntyre said:
>On Thu, 14 Sep 2006 10:25:27 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>>>Mark McIntyre said:

<snip>

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.

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.
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
Sep 14 '06 #60
On 14 Sep 2006 06:01:13 -0700, in comp.lang.c , "KimmoA"
<ki****@gmail.comwrote:
>Richard Bos wrote:
>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".
Then its either an erratum which the publishers need to correct, or a
piece of false information.
--
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
Sep 14 '06 #61
Richard Bos wrote:
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.
His "special edition" is the hardbound 3rd edition, ISBN0201700735, and
hardly "ancient."

Still,

C(x): x is a valid C program.
P(x): x is a valid C++ program.
\exists C(x) \in P(x), x \in {valid programs}

It doesn't mean anything in terms of a language specification, but
Stroustrup was observing that C++ can be, in some cases, a superset of C.
Sep 14 '06 #62
[Attribs tersificated]

Mark McIntyre said:
Richard Heathfield wrote:
>>Mark McIntyre said:
>>Richard Heathfield wrote:
<snip>
>>>>That's not misleading. It's simply false.

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.

Yes, which is what I said,
RH: "It's simply false."
MM: "Its not..."
RH: "It is"
MM: "Yes, which is what I said..."

Hmmm.
and indeed made exceptionally clear in the
quote that you gratuitously snipped.
Hardly gratuitous. We both know the context.
Whats your point exactly?
Just beyond your grasp, it appears. :-(
You're behaving oddly.
That's certainly an opinion. In /my/ opinion, however, I am behaving evenly.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 14 '06 #63
bjarne wrote:
Argh. My intent as I said in the previous message was to drop this, but
I'd better defend a bit.
Default User wrote:
main()
{
return 0;
}
Is NOT valid C++.

as it is not valid C99. It was valid ARM C++ just as it was valid C89.
But the book didn't say that. It said every program in K&R2 *is* a
valid C++ program, not that they were when the book the came out. Two
different statements.
I think you should do more homework before casting doubt about other
people's statements.
I don't see that much is needed. The statement in the early section of
the book quoted here isn't accurate, although when the topic it covered
explicitly in later chapters it's of course correct.

There is a difference between C++ then and now. It's accurate to say,
"The programs were valid C++" but it's not to say (in a book about ISO
Standard C++), "The programs are valid C++".

This is not a huge deal, although it obviously caused some confusion
for at least one contributor here. I can't imagine me ever putting
together a textbook, let alone getting every single line and statement
crystal clean and unambiguous.

Again, and it bears repeating, my goal is NOT to insult, disparage, or
otherwise cast aspersions on Dr. Stroustrup over a single line in a 900
page text.


Brian (hope we can let her rest now)
Sep 14 '06 #64
jmcgill <jm*****@email.arizona.eduwrites:
Richard Bos wrote:
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.

His "special edition" is the hardbound 3rd edition, ISBN0201700735, and
hardly "ancient."

Still,

C(x): x is a valid C program.
P(x): x is a valid C++ program.
\exists C(x) \in P(x), x \in {valid programs}

It doesn't mean anything in terms of a language specification, but
Stroustrup was observing that C++ can be, in some cases, a superset of C.
But that is not what he said; there were further qualifiers in there
than mere validity and existence. There was a term "well-written"
that appears to have been left out of your formal statement entirely,
which is the crux of the matter.

Nobody disputes that there is a common subset of C and C++, in which
code can be written that it syntactically and semantically valid and
defined according to both languages. The dispute is in the part you
left out, which is that *well-written* valid C is likely to be valid
C++ as well. As can be demonstrated by a trivial counterexample, this
is not true.

Charlton

Sep 14 '06 #65
On Thu, 14 Sep 2006 15:29:12 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>RH: "It's simply false."
MM: "Its not..."
RH: "It is"
MM: "Yes, which is what I said..."

Hmmm.
Have you mistaken me (or even yourself) for Jacob? I can see little
other reason for you deliberately and apparently maliciously not only
misquoting me but also making up quotes.
>Whats your point exactly?

Just beyond your grasp, it appears. :-(
And being offensive is hardly grown up. Still, while we're at it,
bugger off and stop being so silly.
--
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
Sep 14 '06 #66
Mark McIntyre said:
On Thu, 14 Sep 2006 15:29:12 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>>RH: "It's simply false."
MM: "Its not..."
RH: "It is"
MM: "Yes, which is what I said..."

Hmmm.

Have you mistaken me (or even yourself) for Jacob?
No.
I can see little
other reason for you deliberately and apparently maliciously not only
misquoting me but also making up quotes.
No, sir; I have not misquoted you, or at least I have certainly not
/deliberately/ misquoted you, let alone /maliciously/ misquoted you, and I
have not made up any quotes whatsoever. I have elided some of what you've
said in an attempt to make your self-contradiction more obvious, but it's
implicit in what you said originally, so I'm not misrepresenting you.

I'm not sure why you're reacting so hostilely, given that my initial
response was very light-hearted, but I think it would be a good idea to
calm down a little.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 14 '06 #67

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.