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
|
|
0
|
|
|
|
Reply
|
lndresnick (326)
|
2/17/2009 6:29:49 PM |
|
Read it once... seems that's enough
maciek
|
|
0
|
|
|
|
Reply
|
maciek.borzecki (2)
|
2/17/2009 6:34:01 PM
|
|
David Resnick <lndresnick@gmail.com> writes:
[...]
> 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. 8-)}
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.
--
Keith Thompson (The_Other_Keith) kst@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21549)
|
2/17/2009 7:05:43 PM
|
|
Keith Thompson <kst-u@mib.org> writes:
> David Resnick <lndresnick@gmail.com> writes:
> [...]
>> 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. 8-)}
>
> 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.
--
Keith Thompson (The_Other_Keith) kst@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21549)
|
2/17/2009 7:09:27 PM
|
|
On Feb 17, 10:29=A0am, David Resnick <lndresn...@gmail.com> wrote:
> Just curious how often people read the C standard as part of their
> work in professionally writing programs. =A0For 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. =A0And yes, that
> combination is not perfect. =A0I 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. =A0 I was yesterday reviewing some code
> that did make me seek the standard, simplified as:
>
> a -=3D 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. =A0Section 16.5.16.2
> resolved that issue nicely...
>
> n.b. I'm not trying to start a flame war, just was curious. =A0If 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.
|
|
0
|
|
|
|
Reply
|
dcorbit (2696)
|
2/17/2009 7:16:42 PM
|
|
On Feb 17, 2:09=A0pm, Keith Thompson <ks...@mib.org> wrote:
> Keith Thompson <ks...@mib.org> writes:
> > David Resnick <lndresn...@gmail.com> writes:
> > [...]
> >> I was yesterday reviewing some code that did make me seek the
> >> standard, simplified as:
>
> >> a -=3D 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. =A0Section 16.5.16.2
> >> resolved that issue nicely...
> > [...]
>
> > 6.5.16.2 would have resolved it even more nicely. =A08-)}
>
> > I'm curious, though: how would a result of 4 make sense? =A0By "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 -=3D,
as in a -=3D 15; or a -=3D someFunc();
Hence the gestalt I had of the operation was:
a =3D a - 15;
How that applied to
a -=3D <expr>;
Was not entirely clear to me. My guess was that it would be
a =3D a - (<expr>);
But I wasn't sure. Hence I looked it up. Of course what different
people need to look up is different.
-David
|
|
0
|
|
|
|
Reply
|
lndresnick (326)
|
2/17/2009 7:22:27 PM
|
|
On Feb 17, 2:16=A0pm, user923005 <dcor...@connx.com> wrote:
> On Feb 17, 10:29=A0am, David Resnick <lndresn...@gmail.com> wrote:
>
>
>
> > Just curious how often people read the C standard as part of their
> > work in professionally writing programs. =A0For 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. =A0And yes, that
> > combination is not perfect. =A0I 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. =A0 I was yesterday reviewing some code
> > that did make me seek the standard, simplified as:
>
> > a -=3D 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. =A0Section 16.5.16.2
> > resolved that issue nicely...
>
> > n.b. I'm not trying to start a flame war, just was curious. =A0If you
> > want to attack people as pedants or whatever, *please* do so
> > elsewhere.
>
> I read it once. =A0Now 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
|
|
0
|
|
|
|
Reply
|
lndresnick (326)
|
2/17/2009 7:25:06 PM
|
|
David Resnick <lndresnick@gmail.com> writes:
> On Feb 17, 2:09�pm, Keith Thompson <ks...@mib.org> wrote:
>> Keith Thompson <ks...@mib.org> writes:
>> > David Resnick <lndresn...@gmail.com> writes:
>> > [...]
>> >> 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. �8-)}
>>
>> > 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.
--
Keith Thompson (The_Other_Keith) kst@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21549)
|
2/17/2009 7:43:29 PM
|
|
David Resnick wrote:
> 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. [...]
Just a few days ago here in c.l.c. I posted an answer relying on
my "good but not perfect" knowledge -- and got it wrong. Obviously,
I read the Standard less frequently than I ought to ...
Usually, I refer to the Standard for one of three reasons (or
sometimes a combination): To shine some light into the language's
dark corners[1], to settle a fine distinction[2], or to look up
the arguments of a function I rarely use[3]. I don't spend hours
on end sitting by the fire reading the Standard and reciting the
juicier passages aloud for my wife; the behavior this would elicit
would be outside the scope of a language definition and unwelcome
to boot.
[1] Different people have different ideas of which corners are
dark and which are well-lit. For me, all things wchar_t
are benighted, and there are a few other corners where I
suspect the grues lurk.
[2] E.g., is the behavior in situation X undefined or merely
unspecified?
[3] E.g., bsearch(). I confess to a degree of dyxlesia about
functions like calloc() and fread() and fwrite(): I can't
seem to remember which size_t argument is the count and which
is the size, so I have to look 'em up. (Strangely, qsort()
doesn't baffle me this way.)
Situations [1] and [2] often arise in discussions on this forum
but seldom in my own use of C; situation [3] comes up with embarrassing
frequency but thankfully it's a secret between me and my editor.
--
Eric.Sosman@sun.com
|
|
0
|
|
|
|
Reply
|
Eric.Sosman (4228)
|
2/17/2009 7:56:11 PM
|
|
David Resnick wrote:
> 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.
|
|
0
|
|
|
|
Reply
|
jameskuyper (5210)
|
2/17/2009 7:59:50 PM
|
|
David Resnick wrote:
> 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...)
--
"Relax, kick back, and have a nice big frosty mug full of STFU."
-Re:Animator/NANAE/9-3-07
|
|
0
|
|
|
|
Reply
|
byrtrxnd.mollznzyrtoublyt (12)
|
2/17/2009 8:08:02 PM
|
|
David Resnick said:
> Just curious how often people read the C standard as part of their
> work in professionally writing programs.
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.
(Whether I use K&R2 or the Standard basically depends on whichever
of them seems less like hard work at the time.)
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.
<snip>
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
|
|
0
|
|
|
|
Reply
|
rjh (10789)
|
2/17/2009 8:31:49 PM
|
|
On Tue, 17 Feb 2009 11:05:43 -0800, Keith Thompson
<kst-u@mib.org> wrote:
>David Resnick <lndresnick@gmail.com> writes:
>[...]
>> 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. 8-)}
>
>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.
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;
You need an explanation of how op= works, either from a standard
or a text. It's easy enough to fall into the misunderstanding
(or forget how it works) because one almost never sees or uses
the kind of code in question. As a pragmatic matter you putting
in parentheses saves time wasted on minutiae, i.e.,
a -= (b-1);
doesn't raise any "what does that do" responses.
Richard Harter, cri@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
|
|
0
|
|
|
|
Reply
|
cri (1432)
|
2/17/2009 8:32:49 PM
|
|
On Feb 17, 3:31=A0pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> David Resnick said:
>
> > Just curious how often people read the C standard as part of their
> > work in professionally writing programs.
>
> 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
|
|
0
|
|
|
|
Reply
|
lndresnick (326)
|
2/17/2009 9:49:16 PM
|
|
David Resnick <lndresnick@gmail.com> writes:
> 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.
--
Ben Pfaff
http://benpfaff.org
|
|
0
|
|
|
|
Reply
|
blp (3953)
|
2/17/2009 9:50:25 PM
|
|
"Richard Heathfield" <rjh@see.sig.invalid> wrote in message
news:0O-dnYthlvsjgAbUnZ2dnUVZ8vmdnZ2d@bt.com...
> David Resnick said:
>
>> Just curious how often people read the C standard as part of their
>> work in professionally writing programs.
> 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.)
--
Bartc
|
|
0
|
|
|
|
Reply
|
bartc (783)
|
2/17/2009 10:48:45 PM
|
|
David Resnick wrote:
> I do aim when writing code to produce perfectly portable C where
> possible/reasonable,
I often wonder about these strictly conforming ISO C programs that
people on this newsgroup supposedly get paid to write professionally.
Maybe it's just me, but when I think of a strictly conforming ISO
C program, I think of something that an employer or contractor
could download from someone's homework directory on an .edu. I
simply can't imagine anyone paying someone else to write a strictly
conforming ISO C program of any significant size and complexity.
As you yourself say, you need to deviate from the standard to get
the job done.
> 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...
K&R2 or H&S are better for that kind of thing, IMHO. You don't get
any nice precedence table in the Standard, since as we're continually
reminded on this newsgroup, the Standard deals with a context-free
grammar, not the "precedence" of mere mortals.
Presumably you're referring to 6.5.16.2{3}, which seems to give you
your answer by accident more than anything else. :-)
> 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.
Actually, the Standard is a great tool in the war against pedants, at
least the current pedants on this newsgroup. I've been citing and referring
to the Standard pretty heavily over the last few months, ever since
I came to the realization that the technical information on this
newsgroup can't be taken at face value anymore because of a personality-
based approach to technical accuracy that leaves many statements
uncorrected and uncontested. It never used to be that way on this
newsgroup -- you could always depend on the Lawrence Kirbys of
comp.lang.c to perform indiscriminate smackdowns of nonsense, no
matter what the origin of that nonsense.
Seriously, though, the Standard is an excellent model of precision
once you overcome the initial hurdle of getting all the Standardese
terms down pat. My only complaint against the Standard is more of
a complaint against the English language than anything else. I've
come to the gradual realization that the Standard should include
a formal semantics of some kind. I know C isn't some ambitious
functional language, but neither was ALGOL, and look what was possible
with it (heh, heh).
Yours,
Han from China
--
"Only entropy comes easy." -- Anton Chekhov
|
|
0
|
|
|
|
Reply
|
autistic-pedantry (1030)
|
2/18/2009 12:07:11 AM
|
|
David Resnick wrote:
>
> 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.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
2/18/2009 1:30:24 AM
|
|
Richard Harter wrote:
>
.... 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;
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
2/18/2009 1:36:54 AM
|
|
Bartc wrote:
>
.... snip ...
>
> (I take it C doesn't have keyword parameters generally available.)
^^^^^^^^^^^^^^^^^^^^
Delete the underlined portion.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
2/18/2009 1:42:17 AM
|
|
"CBFalconer" <cbfalconer@yahoo.com> wrote in message
news:499B6779.6BBB4B91@yahoo.com...
> Bartc wrote:
>>
> ... snip ...
>>
>> (I take it C doesn't have keyword parameters generally available.)
> ^^^^^^^^^^^^^^^^^^^^
> 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.)
--
Bartc
|
|
0
|
|
|
|
Reply
|
bartc (783)
|
2/18/2009 2:10:20 AM
|
|
CBFalconer <cbfalconer@yahoo.com> writes:
> Richard Harter wrote:
>>
> ... 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;
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.
--
Keith Thompson (The_Other_Keith) kst@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|
|
0
|
|
|
|
Reply
|
kst-u (21549)
|
2/18/2009 2:31:36 AM
|
|
On Tue, 17 Feb 2009 18:31:36 -0800, Keith Thompson
<kst-u@mib.org> wrote:
>CBFalconer <cbfalconer@yahoo.com> writes:
>> Richard Harter wrote:
>>>
>> ... 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;
>
>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.
Thank you for explaining things to CBF. I was giggling too hard
to compose a reply.
Richard Harter, cri@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
|
|
0
|
|
|
|
Reply
|
cri (1432)
|
2/18/2009 4:30:37 AM
|
|
Keith Thompson wrote:
> David Resnick <lndresnick@gmail.com> writes:
>> On Feb 17, 2:09 pm, Keith Thompson <ks...@mib.org> wrote:
>>> Keith Thompson <ks...@mib.org> writes:
>>>> David Resnick <lndresn...@gmail.com> writes:
>>>> [...]
>>>>> 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. 8-)}
>>>> 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.
--
Flash Gordon
|
|
0
|
|
|
|
Reply
|
smap (838)
|
2/18/2009 6:48:57 AM
|
|
On 17 Feb, 20:31, Richard Heathfield <r...@see.sig.invalid> wrote:
> David Resnick said:
> > Just curious how often people read the C standard as part of their
> > work in professionally writing programs.
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>
|
|
0
|
|
|
|
Reply
|
nick_keighley_nospam (4575)
|
2/18/2009 10:29:55 AM
|
|
Ben Pfaff <blp@cs.stanford.edu> wrote:
> David Resnick <lndresnick@gmail.com> writes:
>
> > 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.
Ditto, on those days on which I write C (which isn't every day).
Richard
|
|
0
|
|
|
|
Reply
|
raltbos (821)
|
2/18/2009 11:56:06 AM
|
|
On Tue, 17 Feb 2009 20:36:54 -0500, CBFalconer wrote:
> Richard Harter wrote:
>>
> ... 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;
It's already pointed out that Richard Harter didn't expect otherwise, but
I'd like to point out that a - (b - 1) and a - b + 1 are not equivalent
either. If a and b are integer types, a - b + 1 may cause an overflow
where a - (b - 1) doesn't. If a is a pointer type, and a - (b - 1) points
to the start of an object, a - b + 1 is as invalid as a - b. If a and b
are floating point types, a - (b - 1) and a - b + 1 may even give
different results when both expressions are perfectly valid, for example
when a and b are both 1E99 and 1E99 - 1 == 1E99 because the exact result
is not representable.
|
|
0
|
|
|
|
Reply
|
truedfx (1926)
|
2/18/2009 6:21:53 PM
|
|
Keith Thompson wrote:
> David Resnick <lndresnick@gmail.com> writes:
> [...]
> > 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. 8-)}
There is another frequent misconception illustrated by the following code
fragment.
E1 -= E2; is E1 = E1 - (E2);
but the side effects of E1 are only evaluated once so
*(pointer++) -= 1 is the same as *pointer = *(pointer++) - 1
Regards,
--
Walter Banks
Byte Craft Limited
http://www.bytecraft.com
|
|
0
|
|
|
|
Reply
|
walter20 (874)
|
2/18/2009 10:01:52 PM
|
|
In article <gnhjk1$o1c$1@news.motzarella.org>,
Harald van Dijk <truedfx@gmail.com> wrote:
>I'd like to point out that a - (b - 1) and a - b + 1 are not equivalent
>either. If a and b are integer types, a - b + 1 may cause an overflow
>where a - (b - 1) doesn't.
What will happen if b is the minimum value for its type?
|
|
0
|
|
|
|
Reply
|
ike5 (222)
|
2/18/2009 10:05:50 PM
|
|
On Wed, 18 Feb 2009 22:05:50 +0000, Ike Naar wrote:
> In article <gnhjk1$o1c$1@news.motzarella.org>, Harald van Dijk
> <truedfx@gmail.com> wrote:
>>I'd like to point out that a - (b - 1) and a - b + 1 are not equivalent
>>either. If a and b are integer types, a - b + 1 may cause an overflow
>>where a - (b - 1) doesn't.
>
> What will happen if b is the minimum value for its type?
Assuming b is a signed integer type not narrower than int, the behaviour
of a - (b - 1) is undefined, and depending on the type and value of a,
the behaviour of a - b + 1 may or may not be, so that is another similar
way in which they are not equivalent.
|
|
0
|
|
|
|
Reply
|
truedfx (1926)
|
2/18/2009 10:15:51 PM
|
|
On Wed, 18 Feb 2009 17:01:52 -0500, Walter Banks wrote:
> There is another frequent misconception illustrated by the following
> code fragment.
>
> E1 -= E2; is E1 = E1 - (E2);
> but the side effects of E1 are only evaluated once so
>
> *(pointer++) -= 1 is the same as *pointer = *(pointer++) - 1
There's no guarantee in
*pointer = *(pointer++) - 1
that the first reference pointer is evaluated before the increment
starts. A common misconception is even that the RHS of an assignment must
be fully evaluated before the LHS is. This is not required, but certainly
allowed, and if this is done, the two expressions behave significantly
differently.
|
|
0
|
|
|
|
Reply
|
truedfx (1926)
|
2/18/2009 10:23:28 PM
|
|
In article <gni1an$ubn$2@news.motzarella.org>,
Harald van Dijk <truedfx@gmail.com> wrote:
>On Wed, 18 Feb 2009 22:05:50 +0000, Ike Naar wrote:
>> In article <gnhjk1$o1c$1@news.motzarella.org>, Harald van Dijk
>> <truedfx@gmail.com> wrote:
>>>I'd like to point out that a - (b - 1) and a - b + 1 are not equivalent
>>>either. If a and b are integer types, a - b + 1 may cause an overflow
>>>where a - (b - 1) doesn't.
>>
>> What will happen if b is the minimum value for its type?
>
>Assuming b is a signed integer type not narrower than int, the behaviour
>of a - (b - 1) is undefined, and depending on the type and value of a,
>the behaviour of a - b + 1 may or may not be, so that is another similar
>way in which they are not equivalent.
Thank you; I was puzzled by a remark in your previous post that
could be read in two ways. You wrote:
>>> If a and b are integer types, a - b + 1 may cause an overflow
>>> where a - (b - 1) doesn't.
which I first interpreted as:
- there exist a, b, such that a - b + 1 will overflow,
- for all a, b : a - (b - 1) will not overflow
(the last assertion is wrong, e.g. for b == XXX_MIN)
but now it looks like your intention was:
- there exist a, b, such that a - b + 1 will overflow but a - (b - 1) will not.
which is correct.
Sorry for the confusion.
|
|
0
|
|
|
|
Reply
|
ike5 (222)
|
2/18/2009 10:55:28 PM
|
|
Walter Banks wrote:
>
.... snip ...
>
> There is another frequent misconception illustrated by the
> following code fragment.
>
> E1 -= E2; is E1 = E1 - (E2);
> but the side effects of E1 are only evaluated once so
>
> *(pointer++) -= 1 is the same as
> *pointer = *(pointer++) - 1
However that second line exhibits undefined behavior. I believe
the first line is legitimate. The point here is not single
occurence of side effects, but sequence. I would avoid either.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
2/19/2009 1:34:35 AM
|
|
Bartc <bartc@freeuk.com> wrote [re. keyword parameters]:
>
> Oh, I thought I remembered seeing them in connection with lccwin32.
lcc-win32 has a number of extensions that are not standard C.
--
Larry Jones
They say winning isn't everything, and I've decided
to take their word for it. -- Calvin
|
|
0
|
|
|
|
Reply
|
lawrence.jones2 (565)
|
2/19/2009 3:42:11 AM
|
|
Harald van D?k wrote:
> On Wed, 18 Feb 2009 17:01:52 -0500, Walter Banks wrote:
> > There is another frequent misconception illustrated by the following
> > code fragment.
> >
> > E1 -= E2; is E1 = E1 - (E2);
> > but the side effects of E1 are only evaluated once so
> >
> > *(pointer++) -= 1 is the same as *pointer = *(pointer++) - 1
>
> There's no guarantee in
>
> *pointer = *(pointer++) - 1
>
> that the first reference pointer is evaluated before the increment
> starts. A common misconception is even that the RHS of an assignment must
> be fully evaluated before the LHS is. This is not required, but certainly
> allowed, and if this is done, the two expressions behave significantly
> differently.
Might be an interesting survey here. Which compilers
behave as
*pointer = *(pointer++) - 1
or
*(pointer++) = *pointer - 1
From my reading of the standard I believe the first is correct
Regards,
--
Walter Banks
Byte Craft Limited
http://www.bytecraft.com
|
|
0
|
|
|
|
Reply
|
walter20 (874)
|
2/19/2009 3:44:17 PM
|
|
On Thu, 19 Feb 2009 10:44:17 -0500, Walter Banks wrote:
> Harald van D?k wrote:
>> On Wed, 18 Feb 2009 17:01:52 -0500, Walter Banks wrote:
>> > There is another frequent misconception illustrated by the following
>> > code fragment.
>> >
>> > E1 -= E2; is E1 = E1 - (E2);
>> > but the side effects of E1 are only evaluated once so
>> >
>> > *(pointer++) -= 1 is the same as *pointer = *(pointer++) -
>> > 1
>>
>> There's no guarantee in
>>
>> *pointer = *(pointer++) - 1
>>
>> that the first reference pointer is evaluated before the increment
>> starts. A common misconception is even that the RHS of an assignment
>> must be fully evaluated before the LHS is. This is not required, but
>> certainly allowed, and if this is done, the two expressions behave
>> significantly differently.
>
> Might be an interesting survey here. Which compilers
>
> behave as
> *pointer = *(pointer++) - 1
> or
> *(pointer++) = *pointer - 1
>
> From my reading of the standard I believe the first is correct
My point was that
*(pointer++) -= 1
has a defined meaning, whereas
*pointer = *(pointer++) - 1
and
*(pointer++) = *pointer - 1
are undefined because they both read and modify pointer without an
intervening sequence point. So either can be equivalent to the original,
depending on the compiler, the options, and the phase of the moon. Your
understanding of what *(pointer++) -= 1 means is correct, as far as I can
tell, and it's just that your replacement version doesn't mean what you
seem to think it does.
|
|
0
|
|
|
|
Reply
|
truedfx (1926)
|
2/19/2009 6:14:49 PM
|
|
On Thu, 19 Feb 2009 10:44:17 -0500, Walter Banks wrote:
> Harald van D?k wrote:
>> On Wed, 18 Feb 2009 17:01:52 -0500, Walter Banks wrote:
>> > There is another frequent misconception illustrated by the following
>> > code fragment.
>> >
>> > E1 -= E2; is E1 = E1 - (E2);
>> > but the side effects of E1 are only evaluated once so
>> >
>> > *(pointer++) -= 1 is the same as *pointer = *(pointer++) -
>> > 1
>>
>> There's no guarantee in
>>
>> *pointer = *(pointer++) - 1
>>
>> that the first reference pointer is evaluated before the increment
>> starts. A common misconception is even that the RHS of an assignment
>> must be fully evaluated before the LHS is. This is not required, but
>> certainly allowed, and if this is done, the two expressions behave
>> significantly differently.
>
> Might be an interesting survey here. Which compilers
>
> behave as
> *pointer = *(pointer++) - 1
> or
> *(pointer++) = *pointer - 1
>
> From my reading of the standard I believe the first is correct
My point was that
*(pointer++) -= 1
has a defined meaning, whereas
*pointer = *(pointer++) - 1
and
*(pointer++) = *pointer - 1
are undefined because they both read and modify pointer without an
intervening sequence point. So either can be equivalent to the original,
depending on the compiler, the options, and the phase of the moon. Your
understanding of what *(pointer++) -= 1 means is correct, as far as I can
tell, and it's just that your replacement version doesn't mean what you
seem to think it does.
|
|
0
|
|
|
|
Reply
|
truedfx (1926)
|
2/19/2009 6:15:19 PM
|
|
Harald van D?k wrote:
> On Thu, 19 Feb 2009 10:44:17 -0500, Walter Banks wrote:
> > Harald van D?k wrote:
> >> On Wed, 18 Feb 2009 17:01:52 -0500, Walter Banks wrote:
> >> > There is another frequent misconception illustrated by the following
> >> > code fragment.
> >> >
> >> > E1 -= E2; is E1 = E1 - (E2);
> >> > but the side effects of E1 are only evaluated once so
> >> >
> >> > *(pointer++) -= 1 is the same as *pointer = *(pointer++) -
> >> > 1
> >>
> >> There's no guarantee in
> >>
> >> *pointer = *(pointer++) - 1
> >>
> >> that the first reference pointer is evaluated before the increment
> >> starts. A common misconception is even that the RHS of an assignment
> >> must be fully evaluated before the LHS is. This is not required, but
> >> certainly allowed, and if this is done, the two expressions behave
> >> significantly differently.
> >
> > Might be an interesting survey here. Which compilers
> >
> > behave as
> > *pointer = *(pointer++) - 1
> > or
> > *(pointer++) = *pointer - 1
> >
> > From my reading of the standard I believe the first is correct
>
> My point was that
>
> *(pointer++) -= 1
>
> has a defined meaning,
Okay we are on the same page. The standard is clear that the -= expansion doesn't
suddenly create a sequence point issue or problem.
w..
|
|
0
|
|
|
|
Reply
|
walter20 (874)
|
2/19/2009 6:33:32 PM
|
|
David Resnick wrote:
> Just curious how often people read the C standard as part of their
> work in professionally writing programs.
I've been thinking about this. I much more likely to read my implementations
standard than I am the ISO standard, because, lets face it, I am willing to
impose a limit on my code of the machines it will run on.
|
|
0
|
|
|
|
Reply
|
gldncagrls (469)
|
2/20/2009 12:03:53 AM
|
|
Eric Sosman <Eric.Sosman@sun.com> writes:
> Usually, I refer to the Standard for one of three reasons (or
> sometimes a combination): To shine some light into the language's
> dark corners[1], to settle a fine distinction[2], or to look up
> the arguments of a function I rarely use[3]. [snip]
>
> [3] E.g., bsearch(). I confess to a degree of dyxlesia about
^^^^^^^^
> functions like calloc() and fread() and fwrite(): I can't
> seem to remember which size_t argument is the count and which
> is the size, so I have to look 'em up. (Strangely, qsort()
> doesn't baffle me this way.)
Sorry, I couldn't help laughing when I read this.
One of the best developers I ever met was (and presumably still
is) dyslexic. I never understood (until later) why his homeworks
were written on the back side of the paper; couldn't spell in
many cases to save his life either. But he sure could write code.
|
|
0
|
|
|
|
Reply
|
txr (1104)
|
2/21/2009 12:39:09 AM
|
|
Richard Heathfield <rjh@see.sig.invalid> writes:
> David Resnick said:
>
> > Just curious how often people read the C standard as part of their
> > work in professionally writing programs.
>
> 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.
>
> (Whether I use K&R2 or the Standard basically depends on whichever
> of them seems less like hard work at the time.)
>
> 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.
Thinking of the arguments as count and size:
calloc() takes these arguments in alphabetical order
fread(), fwrite() take these arguments in reverse alphabetical order
Another way to think of (count,size) is as dimensions of
a two-dimensional array of type char
char space[nmemb][size]; // nmemb blocks, each with size char's
Again calloc() takes its arguments in the same two-D order,
and fread(), fwrite() take these arguments in reverse two-D order.
Oh, one more thing: Mt Fuji is 12,365 feet high -- 12 months in
a year, 365 days in a year. Another useless (and wrong, so I've
been told) fact, burned into my brain through a memorable mnemonic.
|
|
0
|
|
|
|
Reply
|
txr (1104)
|
2/21/2009 12:49:42 AM
|
|
Flash Gordon <smap@spam.causeway.com> writes:
>>> [...snip...] a -= b-1; [...snip...]
>
> 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.
My memory is that both these points were covered in K&R1,
because later I never felt any need to look them up,
except to be sure my memory was right.
|
|
0
|
|
|
|
Reply
|
txr (1104)
|
2/21/2009 12:56:38 AM
|
|
On Feb 20, 4:39=A0pm, Tim Rentsch <t...@alumnus.caltech.edu> wrote:
> Eric Sosman <Eric.Sos...@sun.com> writes:
> > =A0 =A0 =A0Usually, I refer to the Standard for one of three reasons (o=
r
> > sometimes a combination): To shine some light into the language's
> > dark corners[1], to settle a fine distinction[2], or to look up
> > the arguments of a function I rarely use[3]. =A0[snip]
>
> > =A0 =A0 =A0[3] E.g., bsearch(). =A0I confess to a degree of dyxlesia ab=
out
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ^^^^^^^^
>
> > =A0 =A0 =A0 =A0 =A0functions like calloc() and fread() and fwrite(): I =
can't
> > =A0 =A0 =A0 =A0 =A0seem to remember which size_t argument is the count =
and which
> > =A0 =A0 =A0 =A0 =A0is the size, so I have to look 'em up. =A0(Strangely=
, qsort()
> > =A0 =A0 =A0 =A0 =A0doesn't baffle me this way.)
>
> Sorry, I couldn't help laughing when I read this.
>
> One of the best developers I ever met was (and presumably still
> is) dyslexic. =A0I never understood (until later) why his homeworks
> were written on the back side of the paper; =A0couldn't spell in
> many cases to save his life either. =A0But he sure could write code.
I'm dyslexic. I guess with his spelling problems, he had the correct
letters but put them in the wrong place sometimes. I especially do
this a lot when I look up from the paper as I am writing by hand.
E.g.:
bule for 'blue' or hte for 'the', etc.
Normally, I only get the wrong letters {outside of permutations} when
I hit the wrong keys when typing, and almost never when printing by
hand.
As far as the back side of the paper goes...
I approach glass doors slowly and think before I yank because I can
read the writing on the other side that says 'PULL' quite naturally
and so I may yank when I ought to push. I can read a newspaper in a
mirror about as easily as I can read it normally. There is a strange
effect where things that rotate or translate seem very much the same.
For instance, these are all the same letter (everyone else has it
wrong):
b,d,p,q
|
|
0
|
|
|
|
Reply
|
dcorbit (2696)
|
2/21/2009 1:01:53 AM
|
|
Walter Banks <walter@bytecraft.com> writes:
> Keith Thompson wrote:
>
> > David Resnick <lndresnick@gmail.com> writes:
> > [...]
> > > 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. 8-)}
>
> There is another frequent misconception illustrated by the following code
> fragment.
>
> E1 -= E2; is E1 = E1 - (E2);
> but the side effects of E1 are only evaluated once so
>
> *(pointer++) -= 1 is the same as *pointer = *(pointer++) - 1
The statement
E1 -= E2;
is the same as
(E1_p = &E1, 0) & (E2_v = E2, 0), *E1_p = *E1_p - E2_v;
for suitably typed E1_p and E2_v.
|
|
0
|
|
|
|
Reply
|
txr (1104)
|
2/21/2009 1:01:55 AM
|
|
Walter Banks <walter@bytecraft.com> writes:
> Harald van D?k wrote:
>
> > On Wed, 18 Feb 2009 17:01:52 -0500, Walter Banks wrote:
> > > There is another frequent misconception illustrated by the following
> > > code fragment.
> > >
> > > E1 -= E2; is E1 = E1 - (E2);
> > > but the side effects of E1 are only evaluated once so
> > >
> > > *(pointer++) -= 1 is the same as *pointer = *(pointer++) - 1
> >
> > There's no guarantee in
> >
> > *pointer = *(pointer++) - 1
> >
> > that the first reference pointer is evaluated before the increment
> > starts. A common misconception is even that the RHS of an assignment must
> > be fully evaluated before the LHS is. This is not required, but certainly
> > allowed, and if this is done, the two expressions behave significantly
> > differently.
>
> Might be an interesting survey here. Which compilers
>
> behave as
> *pointer = *(pointer++) - 1
> or
> *(pointer++) = *pointer - 1
>
> From my reading of the standard I believe the first is correct
Disregarding the problem with undefined behavior in both "expansions",
it would be very surprising if any C compiler that's been used by
any significant group of people gets this wrong.
|
|
0
|
|
|
|
Reply
|
txr (1104)
|
2/21/2009 1:07:16 AM
|
|
Tim Rentsch wrote:
> Walter Banks <walter@bytecraft.com> writes:
>
>> Keith Thompson wrote:
>>
>>> David Resnick <lndresnick@gmail.com> writes:
>>> [...]
>>>> 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. 8-)}
>> There is another frequent misconception illustrated by the following code
>> fragment.
>>
>> E1 -= E2; is E1 = E1 - (E2);
>> but the side effects of E1 are only evaluated once so
>>
>> *(pointer++) -= 1 is the same as *pointer = *(pointer++) - 1
>
> The statement
>
> E1 -= E2;
>
> is the same as
>
> (E1_p = &E1, 0) & (E2_v = E2, 0), *E1_p = *E1_p - E2_v;
>
> for suitably typed E1_p and E2_v.
*(pointer++) -= 1 is the same as (--*pointer++)
--
pete
|
|
0
|
|
|
|
Reply
|
pfiland (6614)
|
2/21/2009 1:56:45 AM
|
|
user923005 wrote:
>
.... snip ...
>
> As far as the back side of the paper goes...
> I approach glass doors slowly and think before I yank because I
> can read the writing on the other side that says 'PULL' quite
> naturally and so I may yank when I ought to push. I can read a
> newspaper in a mirror about as easily as I can read it normally.
> There is a strange effect where things that rotate or translate
> seem very much the same. For instance, these are all the same
> letter (everyone else has it wrong):
> b,d,p,q
Interesting. I conclude that most dyslexics are not obvious, but
may be detected by apparent slowness in making some elementary
decisions. Maybe that 'most' should be 'many'.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
2/21/2009 3:54:07 AM
|
|
CBFalconer wrote:
> user923005 wrote:
> .... snip ...
>> As far as the back side of the paper goes...
>> I approach glass doors slowly and think before I yank because I
>> can read the writing on the other side that says 'PULL' quite
>> naturally and so I may yank when I ought to push. I can read a
>> newspaper in a mirror about as easily as I can read it normally.
>> There is a strange effect where things that rotate or translate
>> seem very much the same. For instance, these are all the same
>> letter (everyone else has it wrong):
>> b,d,p,q
>
> Interesting. I conclude that most dyslexics are not obvious,
Of course not. We can't spell Dyslexic so we can't get it tattooed on
our foreheads!
> but
> may be detected by apparent slowness in making some elementary
> decisions. Maybe that 'most' should be 'many'.
I've yet too see anyone other than you decide that Dyslexics might
appear slow at making decisions, elementary or otherwise. As to what
user923005 wrote, plenty of non-dyslexics pull when they should push or
vice-versa, so that would not be very good as a way of detecting Dyslexics.
--
Flash Gordon,
The Dyslexic atheist was not sure of there was a bog.
|
|
0
|
|
|
|
Reply
|
smap (838)
|
2/21/2009 8:16:13 AM
|
|
cri@tiac.net (Richard Harter) writes:
> [snip]
>
> As a pragmatic matter you putting
> in parentheses saves time wasted on minutiae, i.e.,
>
> a -= (b-1);
>
> doesn't raise any "what does that do" responses.
Although I wouldn't try to actively prevent such things,
I wonder what the effect of such practices is on the
software community. It seems to me the profession
would benefit if there were an expectation that every
non-beginning programmer knew the rules for cases
like this one, especially for environments as small
and well-defined as the C language.
|
|
0
|
|
|
|
Reply
|
txr (1104)
|
2/21/2009 12:57:31 PM
|
|
On 21 Feb 2009 04:57:31 -0800, Tim Rentsch
<txr@alumnus.caltech.edu> wrote:
>cri@tiac.net (Richard Harter) writes:
>
>> [snip]
>>
>> As a pragmatic matter you putting
>> in parentheses saves time wasted on minutiae, i.e.,
>>
>> a -= (b-1);
>>
>> doesn't raise any "what does that do" responses.
>
>Although I wouldn't try to actively prevent such things,
>I wonder what the effect of such practices is on the
>software community.
I opine that the effect is to reduce coding errors and increase
programming productivity.
>It seems to me the profession
>would benefit if there were an expectation that every
>non-beginning programmer knew the rules for cases
>like this one, especially for environments as small
>and well-defined as the C language.
I would not call the C language well defined; rather it is
precisely defined.
Richard Harter, cri@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
|
|
0
|
|
|
|
Reply
|
cri (1432)
|
2/21/2009 4:14:09 PM
|
|
cri@tiac.net (Richard Harter) writes:
> On 21 Feb 2009 04:57:31 -0800, Tim Rentsch
> <txr@alumnus.caltech.edu> wrote:
>
> >cri@tiac.net (Richard Harter) writes:
> >
> >> [snip]
> >>
> >> As a pragmatic matter you putting
> >> in parentheses saves time wasted on minutiae, i.e.,
> >>
> >> a -= (b-1);
> >>
> >> doesn't raise any "what does that do" responses.
> >
> >Although I wouldn't try to actively prevent such things,
> >I wonder what the effect of such practices is on the
> >software community.
>
> I opine that the effect is to reduce coding errors and increase
> programming productivity.
Yes, and that might very well be the effect. But it would be
nice to do some experiments, at least on a local scale, and get
some actual data. Call me old fashioned, but I believe in the
power of science.
> >It seems to me the profession
> >would benefit if there were an expectation that every
> >non-beginning programmer knew the rules for cases
> >like this one, especially for environments as small
> >and well-defined as the C language.
>
> I would not call the C language well defined; rather it is
> precisely defined.
I meant "well-defined" in the sense that there is pretty
widespread agreement as to what the definition is, not any
kind of formal statement about how good a definition it is.
So my use of "well-defined" was not well-defined, one might
say.
|
|
0
|
|
|
|
Reply
|
txr (1104)
|
2/21/2009 4:43:45 PM
|
|
Flash Gordon wrote:
> CBFalconer wrote:
>> user923005 wrote:
>>
>> ... snip ...
>>
>>> As far as the back side of the paper goes...
>>> I approach glass doors slowly and think before I yank because I
>>> can read the writing on the other side that says 'PULL' quite
>>> naturally and so I may yank when I ought to push. I can read a
>>> newspaper in a mirror about as easily as I can read it normally.
>>> There is a strange effect where things that rotate or translate
>>> seem very much the same. For instance, these are all the same
>>> letter (everyone else has it wrong):
>>> b,d,p,q
>>
>> Interesting. I conclude that most dyslexics are not obvious,
>
> Of course not. We can't spell Dyslexic so we can't get it
> tattooed on our foreheads!
>
>> but may be detected by apparent slowness in making some
>> elementary decisions. Maybe that 'most' should be 'many'.
>
> I've yet too see anyone other than you decide that Dyslexics
> might appear slow at making decisions, elementary or otherwise.
> As to what user923005 wrote, plenty of non-dyslexics pull when
> they should push or vice-versa, so that would not be very good
> as a way of detecting Dyslexics.
Why are you getting upset at my comments? I am not dyslexic,
neither is any of my family, or acquaintances (to my knowledge).
So I have zero experience. I don't expect to classify by reading
forehead tattoes, but it might be worthwhile to know when to make
allowances.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
2/22/2009 1:32:30 AM
|
|
On 22 Feb 2009 at 1:32, CBFalconer wrote:
> Why are you getting upset at my comments?
It's amazing how thoughtless, wrong-headed and offensive comments are
able to upset people, isn't it?
> I am not dyslexic, neither is any of my family, or acquaintances (to
> my knowledge).
Not even your Nazi aunt?
Still, it's good to know that we can add dyslexia to the list of topical
subjects on clc according to the "regulars".
|
|
0
|
|
|
|
Reply
|
nospam59 (9950)
|
2/22/2009 1:36:50 PM
|
|
Tim Rentsch wrote:
> Eric Sosman <Eric.Sosman@sun.com> writes:
>>
>> [3] E.g., bsearch(). I confess to a degree of dyxlesia about
> ^^^^^^^^
> Sorry, I couldn't help laughing when I read this.
Well, I'm glad *somebody* was paying attention! It's always
discouraging when an attempted witticism elicits no laughs. Makes
a wit feel like a halfwit, it does.
--
Eric.Sosman@sun.com
|
|
0
|
|
|
|
Reply
|
Eric.Sosman (4228)
|
2/23/2009 3:43:31 PM
|
|
Eric Sosman wrote:
> Tim Rentsch wrote:
>> Eric Sosman <Eric.Sosman@sun.com> writes:
>>>
>>> [3] E.g., bsearch(). I confess to a degree of dyxlesia about
>> ^^^^^^^^
>> Sorry, I couldn't help laughing when I read this.
>
> Well, I'm glad *somebody* was paying attention!
I'd have spotted it, except that I am in fact mildly dyxlesic...
especially when typing teh word eth in ah urry. :-)
(joking aside, mild dyslexia runs in my family and I have a devil of a
job spotting small errors like that. Thank goodness for spill chuckers).
|
|
0
|
|
|
|
Reply
|
markmcintyre2 (407)
|
2/24/2009 11:21:44 PM
|
|
Golden California Girls <gldncagrls@aol.com.mil> wrote:
> David Resnick wrote:
> > Just curious how often people read the C standard as part of their
> > work in professionally writing programs.
>
> I've been thinking about this. I much more likely to read my implementations
> standard than I am the ISO standard, because, lets face it, I am willing to
> impose a limit on my code of the machines it will run on.
Are you also willing to impose a limit on which version of your compiler
you're willing to use? Five years from now?
Richard
|
|
0
|
|
|
|
Reply
|
raltbos (821)
|
2/25/2009 1:22:46 PM
|
|
On Tue, 17 Feb 2009 17:07:11 -0700 (MST), Han from China
<autistic-pedantry@comp.lang.c> wrote:
<snip: referring to the standard>
> K&R2 or H&S are better for that kind of thing, IMHO. You don't get
> any nice precedence table in the Standard, since as we're continually
> reminded on this newsgroup, the Standard deals with a context-free
> grammar, not the "precedence" of mere mortals.
>
Subclauses 6.5.* are in descending order of precedence, or as close as
is possible given the two small non-total-orderings, so the table of
contents contains a precedence table. (And the acrord version I have,
on the document version I have, conveniently puts toc-equiv in a
popup, although I realize that isn't universally true.)
And the summary in A.2 (including A.2.1) is in document order,
thus descending precedence.
|
|
0
|
|
|
|
Reply
|
dave.thompson2 (767)
|
3/9/2009 4:55:07 AM
|
|
|
56 Replies
21 Views
(page loaded in 1.004 seconds)
|