Re: the 'standard' is so strange
- From: Nate Eldredge <nate@xxxxxxxxxx>
- Date: Mon, 17 Nov 2008 12:50:07 -0800
Pilcrow <Pilcrow6@xxxxxxxxx> writes:
On Mon, 17 Nov 2008 09:18:44 -0800, Keith Thompson <kst-u@xxxxxxx>
wrote:
Pilcrow <Pilcrow6@xxxxxxxxx> writes:
This behavior seems very strange to me, but I imagine that someone will
be able to 'explain' it in terms of the famous C standard.
-------------------- code -----------------------------------
#include <stdio.h>
int main (void)
{
char xx[]="abcd";
char * p1 = xx;
char * p2 = xx;
int i;
for(i = 0; i < 4; i++)
printf("%d %c %c\n", i, *p1++, *p1 );
putchar('\n');
for(i = 0; i < 4; i++)
printf("%d %c %c\n", i, *p2, *p2++);
}
-------------------- output -----------------------------------
0 a a
1 b b
2 c c
3 d d
0 b a
1 c b
2 d c
3 d
-------------------------------------------------------------
Others have done a very good job of answering your question. But I'm
curious: why do you have such a dismissive attitude regarding the C
standard (putting the words "standard" and "explain" in quotation
marks and so forth)?
Before posting my example I had already realised that I was not allowed
(at least in *this* case) to use a shortcut to modify an argument to a
function *within* the parens. I just assumed that the much-cited k&r
had fully defined the language. All these 'undefines' have me feeling
like I'm trying to herd cats. (snakes?)
Even K&R didn't define the issue of passing expressions with side
effects as function arguments. K&R I said "The order of evaluation of
arguments is undefined by the language; take note that the various
compilers differ." By the time K&R got around to writing a formal
description of the language, there were already different compilers
around that disagreed. Rather than demand a certain behavior and make
existing implementations wrong, and require them to switch to behavior
that could be less efficient, they decided (rightly, IMHO) that it
didn't really matter, and left it up to the compiler authors to decide
what to do.
If you want a completely specified language, where every possible piece
of code has a well-defined effect, then C is not for you. One of the
major strengths of the language is its efficiency and ability to run on
many different platforms; the tradeoff for this is that in many cases
the language will say "Programmers: don't do this if you want to be
portable. Compilers: handle this in whatever way you find most
convenient." That's what "undefined behavior" means.
To mix metaphores: just when I think I'm beginning to get a grip on C,
it turns into sea, and runs through my fingers.
That happens a lot when dealing with computers. Sorry.
Now I realise that I will have to read the 'standard', which seems to
have been written by a team composed of Philidelphia lawyers and
abstract mathematicians, neither of whom heard of Henry W Fowler. Truly
gelatinous prose. Technical writing need not be so murky. Oh, well, we
live to learn.
You don't necessarily have to read the standard, though it is the
authoritative source. Any decent textbook should have told you not to
write code that depends on the order in which function arguments are
evaluated.
Maybe I'll try to make a catalog of *all* the 'undefines' and similar
gotchas in C. Written in English.
Thanks to all for a most illuminating experience.
BTW, what's a 'sequence point', and how could I recognize one in a dark
alley? Never mind, maybe the *standard* will tell me.
Maybe you should ask again when you're more calm.
.
- References:
- the 'standard' is so strange
- From: Pilcrow
- Re: the 'standard' is so strange
- From: Keith Thompson
- Re: the 'standard' is so strange
- From: Pilcrow
- the 'standard' is so strange
- Prev by Date: Re: the 'standard' is so strange
- Next by Date: Re: the 'standard' is so strange
- Previous by thread: Re: the 'standard' is so strange
- Next by thread: Re: the 'standard' is so strange
- Index(es):
Relevant Pages
|