How often do you read the C standard?

D

David Resnick

Just curious how often people read the C standard as part of their
work in professionally writing programs. For me the answer is almost
never, I rely on my (good but not perfect) knowledge of what is valid
C backed up by my compiler with all warnings enabled. And yes, that
combination is not perfect. I do aim when writing code to produce
perfectly portable C where possible/reasonable, and where I deviate
from the standard to never do so accidentally, but rather purposefully
with the trade offs considered. I was yesterday reviewing some code
that did make me seek the standard, simplified as:

a -= b - 1;

I looked at that, and realized that I didn't know offhand whether if a
was 10 and b was 5 the result would be 6 or 4. Section 16.5.16.2
resolved that issue nicely...

n.b. I'm not trying to start a flame war, just was curious. If you
want to attack people as pedants or whatever, *please* do so
elsewhere.

-David
 
K

Keith Thompson

David Resnick said:
I was yesterday reviewing some code that did make me seek the
standard, simplified as:

a -= b - 1;

I looked at that, and realized that I didn't know offhand whether if a
was 10 and b was 5 the result would be 6 or 4. Section 16.5.16.2
resolved that issue nicely...
[...]

6.5.16.2 would have resolved it even more nicely. :cool:}

I'm curious, though: how would a result of 4 make sense? By "result",
do you mean the value assigned to a, or the (discarded) result of the
expression?

Personally, I wouldn't have needed to check the standard to know what
that means.
 
K

Keith Thompson

Keith Thompson said:
David Resnick said:
I was yesterday reviewing some code that did make me seek the
standard, simplified as:

a -= b - 1;

I looked at that, and realized that I didn't know offhand whether if a
was 10 and b was 5 the result would be 6 or 4. Section 16.5.16.2
resolved that issue nicely...
[...]

6.5.16.2 would have resolved it even more nicely. :cool:}

I'm curious, though: how would a result of 4 make sense? By "result",
do you mean the value assigned to a, or the (discarded) result of the
expression?

Or both?
Personally, I wouldn't have needed to check the standard to know what
that means.

I realize that might come across as more snarky than I intended.
I'm not trying to show off; I'm just curious about this particular
misunderstanding.
 
U

user923005

Just curious how often people read the C standard as part of their
work in professionally writing programs.  For me the answer is almost
never, I rely on my (good but not perfect) knowledge of what is valid
C backed up by my compiler with all warnings enabled.  And yes, that
combination is not perfect.  I do aim when writing code to produce
perfectly portable C where possible/reasonable, and where I deviate
from the standard to never do so accidentally, but rather purposefully
with the trade offs considered.   I was yesterday reviewing some code
that did make me seek the standard, simplified as:

a -= b - 1;

I looked at that, and realized that I didn't know offhand whether if a
was 10 and b was 5 the result would be 6 or 4.  Section 16.5.16.2
resolved that issue nicely...

n.b. I'm not trying to start a flame war, just was curious.  If you
want to attack people as pedants or whatever, *please* do so
elsewhere.

I read it once. Now I never read it, but I do refer to it from time
to time.
 
D

David Resnick

Keith Thompson said:
David Resnick said:
I was yesterday reviewing some code that did make me seek the
standard, simplified as:
a -= b - 1;
I looked at that, and realized that I didn't know offhand whether if a
was 10 and b was 5 the result would be 6 or 4.  Section 16.5.16.2
resolved that issue nicely... [...]

6.5.16.2 would have resolved it even more nicely.  :cool:}
I'm curious, though: how would a result of 4 make sense?  By "result",
do you mean the value assigned to a, or the (discarded) result of the
expression?

Or both?
Personally, I wouldn't have needed to check the standard to know what
that means.

I realize that might come across as more snarky than I intended.
I'm not trying to show off; I'm just curious about this particular
misunderstanding.

No offense taken. I meant by result is that 4 would be the value
assigned to a. I had only previously come across simpler cases of -=,
as in a -= 15; or a -= someFunc();

Hence the gestalt I had of the operation was:

a = a - 15;

How that applied to

a -= <expr>;

Was not entirely clear to me. My guess was that it would be

a = a - (<expr>);

But I wasn't sure. Hence I looked it up. Of course what different
people need to look up is different.

-David
 
D

David Resnick

I read it once.  Now I never read it, but I do refer to it from time
to time.

By read I meant read in part to resolve a particular question, not re-
read from beginning to end.

-David
 
K

Keith Thompson

David Resnick said:
Keith Thompson said:
[...]
I was yesterday reviewing some code that did make me seek the
standard, simplified as:
a -= b - 1;
I looked at that, and realized that I didn't know offhand whether if a
was 10 and b was 5 the result would be 6 or 4.  Section 16.5.16.2
resolved that issue nicely...
[...]
6.5.16.2 would have resolved it even more nicely.  :cool:}
I'm curious, though: how would a result of 4 make sense?  By "result",
do you mean the value assigned to a, or the (discarded) result of the
expression?

Or both?
Personally, I wouldn't have needed to check the standard to know what
that means.

I realize that might come across as more snarky than I intended.
I'm not trying to show off; I'm just curious about this particular
misunderstanding.

No offense taken. I meant by result is that 4 would be the value
assigned to a. I had only previously come across simpler cases of -=,
as in a -= 15; or a -= someFunc();

Ok, where the RHS is a primary expression.
Hence the gestalt I had of the operation was:

a = a - 15;

How that applied to

a -= <expr>;

Was not entirely clear to me. My guess was that it would be

a = a - (<expr>);

But I wasn't sure. Hence I looked it up. Of course what different
people need to look up is different.

My thought was that there's no consistent interpretation of the "-="
operator that would result in a being assigned the value 4. If "-="
bound more tightly than "-", then
a -= b - 1;
would be equivalent to
(a -= b) - 1;
which set a to 5 (and yield a discarded result of 4).

But I understand that checking the standard is easier and more
reliable than following that line of reasoning *and* being sure that
you haven't missed something.
 
J

jameskuyper

David said:
Just curious how often people read the C standard as part of their
work in professionally writing programs.

I read parts of it several times a week, but mainly to look something
up in connection with messages to be posted to this newsgroup. I use
it professionally only when writing code using C features that I've
familiar with, but haven't made frequent recent use of. That's
something that comes up only a few times per year.
 
B

Bertrand Mollinier Toublet

David said:
Just curious how often people read the C standard as part of their
work in professionally writing programs.

I have had to refer to the standard on a semi-regular basis. However, it
has always been in contexts where source code processing was involved
(i.e. when I was working on a tool that was involved in compiling C,
rather than when writing the code itself...)
 
D

David Resnick

David Resnick said:


Whenever I need to, which isn't very often but does happen from time
to time.

Do you know what I use it for *most*? For looking up whether fread
and fwrite take size, count or count, size.

Do you know what I use K&R2 for most? For looking up whether fread
and fwrite take size, count or count, size.

Interesting. I used to use K&R2 for that type of thing -- in fact, my
book falls open to the PRINTF CONVERSIONS table in the appendix
because I used to go there often. I now tend to use man pages to get
function usage, which is quicker than flipping through a book. The
ones I use (linux, RH5) are annotated with which standard the function
conforms to in case that level of detail is needed.

-David
 
B

Ben Pfaff

David Resnick said:
Just curious how often people read the C standard as part of their
work in professionally writing programs.

I use it as my primary reference for the standard C library, so a
couple of times a day.
 
B

Bartc

Richard Heathfield said:
David Resnick said:
Do you know what I use it for *most*? For looking up whether fread
and fwrite take size, count or count, size.
Why I can't remember that simple detail about fread and fwrite, I
can't tell. But I must have used them hundreds if not thousands of
times over the years, and I still can't keep them straight for more
than about ten minutes at a time.

Create wrappers for fread and fwrite called freadcs and fwritecs, which take
count and size.

And another two wrappers called freadsc and fwritesc which take size and
count. (One of these sets can be just macro substitutions for 'fread' and
'fwrite'; the other can be inline functions.)

(I take it C doesn't have keyword parameters generally available.)
 
C

CBFalconer

David said:
Just curious how often people read the C standard as part of
their work in professionally writing programs. For me the answer
is almost never, I rely on my (good but not perfect) knowledge of
what is valid C backed up by my compiler with all warnings
enabled. And yes, that combination is not perfect. I do aim
when writing code to produce perfectly portable C where
possible/reasonable, and where I deviate from the standard to
never do so accidentally, but rather purposefully with the trade
offs considered. I was yesterday reviewing some code that did
make me seek the standard, simplified as:

a -= b - 1;

I looked at that, and realized that I didn't know offhand whether
if a was 10 and b was 5 the result would be 6 or 4. Section
16.5.16.2 resolved that issue nicely...

Well, I keep n869 available for quick searches (it is the last
version published in text format). I especially use it to check
function parameters, parameter orders, etc. since I can normally
access the appropriate section within about 1 second, including
loading the file. Another handy immediate access is to resolve
printf format strings.
 
C

CBFalconer

Richard said:
.... snip ...

Here's one way. If you think of
var -= text
as shorthand for
var = var - text
then
a -= b -1;
becomes
a = a - b - 1;

Are you sure? I would expect the expansion to be:

a = a - (b - 1);
ie:
a = a - b + 1;
 
B

Bartc

CBFalconer said:
^^^^^^^^^^^^^^^^^^^^
Delete the underlined portion.

Oh, I thought I remembered seeing them in connection with lccwin32. Perhaps
it was Python.

I've used them elsewhere and they are great (if you don't have a fancy IDE
doing this for you). Especially when the actual parameters are 'sparse'.

(Now I'll switch back to my proportional font.)
 
K

Keith Thompson

CBFalconer said:
Are you sure? I would expect the expansion to be:

a = a - (b - 1);
ie:
a = a - b + 1;

Yes, we all know that. Note the word "If" in Richard's article.
We're not discussing the actual semantics of the "-=" operator; that
was established in the original article. The question we're
discussing is, given:

int a = 10;
int b = 5;
a -= b - 1;

why would someone *mistakenly* think that a would be assigned the
value 4?

Here's some sample code that illustrates the issue:

#include <stdio.h>

/* BAD MACRO! */
#define SUBTRACT(x, y) x = x - y

int main(void)
{
int a = 10;
int b = 5;
/* a -= b - 1; */
SUBTRACT(a, b - 1);
printf("a = %d\n", a);
return 0;
}

The output is "a = 4". Someone who incorrectly thought that "-="
worked like a macro might have this same misunderstanding.

Note that the original poster didn't actually make this mistake; he
simply wasn't sure whether a would be assigned the value 6 or 4, and
consulted the standard to confirm his guess.
 
F

Flash Gordon

Keith said:
David Resnick said:
[...]
I was yesterday reviewing some code that did make me seek the
standard, simplified as:
a -= b - 1;
I looked at that, and realized that I didn't know offhand whether if a
was 10 and b was 5 the result would be 6 or 4. Section 16.5.16.2
resolved that issue nicely...
[...]
6.5.16.2 would have resolved it even more nicely. :cool:}
I'm curious, though: how would a result of 4 make sense? By "result",
do you mean the value assigned to a, or the (discarded) result of the
expression?
Or both?

Personally, I wouldn't have needed to check the standard to know what
that means.
I realize that might come across as more snarky than I intended.
I'm not trying to show off; I'm just curious about this particular
misunderstanding.
No offense taken. I meant by result is that 4 would be the value
assigned to a. I had only previously come across simpler cases of -=,
as in a -= 15; or a -= someFunc();

Ok, where the RHS is a primary expression.
Hence the gestalt I had of the operation was:

a = a - 15;

How that applied to

a -= <expr>;

Was not entirely clear to me. My guess was that it would be

a = a - (<expr>);

But I wasn't sure. Hence I looked it up. Of course what different
people need to look up is different.

My thought was that there's no consistent interpretation of the "-="
operator that would result in a being assigned the value 4. If "-="
bound more tightly than "-", then
a -= b - 1;
would be equivalent to
(a -= b) - 1;
which set a to 5 (and yield a discarded result of 4).

A few weeks ago I had an email from a colleague who has been programming
in C certainly since before you could rely on compilers implementing
C89. He, like David, had previously only used a primary expression as
the RHS but this time he did not. He thought that the above was just
shorthand for
a = a - b - 1;
This interpretation leads, of course, to the assumption that "a" will be
assigned the value of 4. Personally I had always understood it correctly
to be
a = a - (b - 1);
I can't remember if when I initially also understood that the LHS is
only evaluated once, but it does seem logical to me now.
But I understand that checking the standard is easier and more
reliable than following that line of reasoning *and* being sure that
you haven't missed something.

I'm more likely to start off by checking K&R2.
 
G

Guest

David Resnick said:

I refer to it from time to time to check things.
I must be over-due for a read because it was pointed out to
me recently that

<expr> ? NULL : <some pointer type>

*must* be of type <some pointer type> due to a special case
in the standard.

Whenever I need to, which isn't very often but does happen from time
to time.

Do you know what I use it for *most*? For looking up whether fread
and fwrite take size, count or count, size.

I used to use the standard to look up library stuff
(I had the appropriate appendix photocopied with some
bits pinned to the wall) these days I use the internet
because I can copy paste it into my code. Fortunatly
most C library functions are so strangely spelt you hardly
ever get collisions with anything else. The only time
I've said "thank <deity> for 6-significant-character linkers!"


<snip>
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top