R
Richard Bos
Ben Pfaff said: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
Ben Pfaff said:I use it as my primary reference for the standard C library, so a
couple of times a day.
Are you sure? I would expect the expansion to be:
a = a - (b - 1);
ie:
a = a - b + 1;
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.}
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?
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
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.
Walter said:.... 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
Bartc said:Oh, I thought I remembered seeing them in connection with lccwin32.
Harald said: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
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
Harald said:My point was that
*(pointer++) -= 1
has a defined meaning,
^^^^^^^^Eric Sosman said: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.)
Richard Heathfield said: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.
(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.
Flash Gordon said:[...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.
Eric Sosman said: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.
Walter Banks said: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.}
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
Walter Banks said: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
user923005 said:.... 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
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.