473,900 Members | 4,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3767

Richard Bos wrote:
"KimmoA" <ki****@gmail.c omwrote:
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

Sep 13 '06 #41
Charlton Wilbur wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>"KimmoA" <ki****@gmail.c omwrote:
>>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++
Sep 13 '06 #42
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.


Brian
Sep 13 '06 #43
Default User wrote:
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.

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

Sep 13 '06 #44
jmcgill <jm*****@email. arizona.eduwrit es:
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
Sep 13 '06 #45
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"KimmoA" <ki****@gmail.c omwrote:
>Keith Thompson wrote:
C and C++ are two different languages.

Yes, but any well-written C is supposed to compile as a C++ program.

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

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Sep 13 '06 #46
jmcgill wrote:
Default User wrote:
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.
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
Sep 13 '06 #47
Charlton Wilbur said:

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

--
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 13 '06 #48
"Default User" <de***********@ yahoo.comwrites :
jmcgill wrote:
>Default User wrote:
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.
>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*.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Sep 13 '06 #49
On 12 Sep 2006 15:19:15 -0700, in comp.lang.c , "KimmoA"
<ki****@gmail.c omwrote:
>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
Sep 13 '06 #50

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

Similar topics

1
2619
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for topics to include in a course on object-orientation that I'm going to conduct. (I will later summarize all the replies and discussion, for the
193
9721
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
19
2216
by: pamelafluente | last post by:
Hi Guys, I am trying to include my little script in my html report. I have done an external JS file which contains it. If you remember, you have helped me to detect if the asp page was present by using ajax. If not present is gracefully stay silent. The problem is now that when the JS is not present (and it can be) MSIE gives a lot of errors and one has to kill it.
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10866
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5891
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.