switch ( expression ) statement
instead of:
switch ( expression ) block
Eric Lemings
>instead of:
A block is a kind-of-a statement. But the key is that you do not need
the {}'s if there is only one case and one statement.
For instance: (One of my entries in IOCCC.)
#include <stdio.h>
#include <stdlib.h>
int
main(int c, char *v[])
{
char *f = "Hello, world!";
switch (c)
case 2:
switch (*v[1] ^ '-')
case 0:
switch (*(v[1] + 1) ^ 't')
case 0:
f = "hello, world!";
((c > 2) || ((c > 1) && !(*v[1] ^ '-') && *(v[1] + 1) ^ 't')) &&
(puts("usage: hello [-h|-t]"),
exit(((c > 2) || (*v[1] ^ '-') ||
(*(v[1] + 1) ^ 'h') || (*(v[1] + 2))) ?
EXIT_FAILURE : EXIT_SUCCESS), 1);
puts(f);
return 0;
}
-s
--
Peter Seebach - se...@solon.com || se...@intran.xerox.com --
C/Unix proto-wizard -- C/Unix questions? Send mail for help. No, really!
Copyright 1995 Peter Seebach. -- High energy particle theology
The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq
>A block is a kind-of-a statement.
True, but this doesn't answer why they did it.
>But the key is that you do not need
>the {}'s if there is only one case and one statement.
True, but this doesn't answer why they did it.
>For instance: (One of my entries in IOCCC.)
Now we know why they did it.
Next question is why they require parentheses when return and sizeof don't
require parentheses around the expression. Next, why did they spell out
the word in full, and why did they waste two keywords (swit and case) when
they could have done it in one.
--
<< If this were the company's opinion, I would not be allowed to post it. >>
"I paid money for this car, I pay taxes for vehicle registration and a driver's
license, so I can drive in any lane I want, and no innocent victim gets to call
the cops just 'cause the lane's not goin' the same direction as me" - J Spammer
From "A brief(ish) description of BCPL" by Clive, it looks like a return value
is specified in BCPL with the RESULTIS command, which apparently doesn't
require parentheses (the RETURN command doesn't specify a return value). Alan
Watson posted an article in comp.lang.c that contains "very old C" that does
not have parentheses around the return-statement operand. Mark Brader posted an
article in comp.lang.c that contains this line in B:
return (f); /* at least, I think the () were required */
[His comment.]
(You guys have any further comment?)
It's interesting to note that in the code examples in K&R1, the return value is
always enclosed in parentheses, whereas in K&R2, the return value is _never_
enclosed in parentheses. The grammar in both versions show that parentheses are
not required.
This is what I bet happened: B may have required parentheses or at least this
was the convention. From the start, C did not require parentheses, but K&R1
showed the world code that had parentheses in return statements. K&R changed
their minds by K&R2, but the public hasn't picked up on the shift yet.
--
Paul Long 45:29:14N 122:48:09W
pl...@perf.com http://www.teleport.com/~pciwww/
"Where time or intelligence are lacking, a goto may do the job."
- M.E. Hopkins, "A Case for the GOTO," 1972.
>This is what I bet happened: B may have required parentheses or at least this
>was the convention. From the start, C did not require parentheses, but K&R1
>showed the world code that had parentheses in return statements. K&R changed
>their minds by K&R2, but the public hasn't picked up on the shift yet.
I don't have K&R in front of me but I did check up on this not so long ago.
If you look up return in the index there are something like 4 references to
it, 3 in the tutorial, one in the language reference section. From the way
return is described in the tutoral gives every indication that the
parentheses are mandatory. Only the syntax in the reference section indicates
that they are not.
But consider somebody learning the language. What is the chance of them
checking up on the precise syntactic details of return in the reference
section (the tutorial seems pretty clear and there isn't much to query).
Even if you read all through the reference section it is easy to miss.
So I reckon K&R created the false notion that parentheses are required. I
learnt C from K&R and I spend years believing that parentheses were
required. I thought the dropping of parentheses was a new-fangled ANSI
thing! :-)
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------
return doesn't require parentheses because it's unambiguous without.
sizeof is unambiguous (by definition, because of its defined precedence
as an operator), but arguably should require parentheses. switch would
be ambiguous: is
switch a * b ;
equivalent to
switch ( a ) * b ;
or
switch ( a * b ) ;
? switch and case can be combined unambiguously, but only because of
the colon required at the end of the case. Making them separate
keywords makes parsing simpler, and makes code easier to understand.
-zefram
The parentheses are required in B. I just checked the manual -- the compiler
might be forgiving. We still maintain (and get paid for) a B compiler!
This posting is a week too late...
--
David Tanguay d...@Thinkage.on.ca http://www.thinkage.on.ca/~dat/
Thinkage, Ltd. Kitchener, Ontario, Canada [43.24N 80.29W]