scs@vax3.iti.org (Steve Simmons) (04/27/89)
A friend was over tonight and we were talking over wierd C code
we have written. The conversation brought up two oddies, of
which this is the first. We used to use an old Altos 8086 box
running v7 (never saw the commercial light of day, I think) where
we wrote code like:
main()
{
int a = 0 ;
a = if ( a == 1 )
12 ;
else
14 ;
printf( "Value of a is %d\n", a ) ;
}
We tried it out on all the C compilers we could find (BSD 4.3, Gould,
UNIX-PC, gcc) and it fails. But the error messages are quite cryptic
(we like gcc: "parse error after 'a'") and largely don't address
the real problem. Anybody else ever use stuff like this?
Steve Simmons Just another midwestern boy
scs@vax3.iti.org -- or -- ...!sharkey!itivax!scs
"Hey...you *can* get here from here!"
raymond@ptolemy.arc.nasa.gov (Eric A. Raymond) (04/27/89)
In article <1043@itivax.iti.org> scs@vax3.iti.org (Steve Simmons) writes: > > a = if ( a == 1 ) > 12 ; > else > 14 ; Sorry, but C is not LISP. That is, compund (or complex) statements do not return values. You can accomplish this behavior via the ?: connstruct: a = (a ? 12 : 14); Incidently, I beleive the comma operator allows you to approach a progn (or is it prog1?): a = (x=1, y=2, z=3); A is 3 if the last expr is returned (progn-like), otherwise 1 (prog1-like). Look it up. -- Eric A. Raymond (raymond@ptolemy.arc.nasa.gov) G7 C7 G7 G#7 G7 G+13 C7 GM7 Am7 Bm7 Bd7 Am7 C7 Do13 G7 C7 G7 D+13: Elmore James
gwyn@smoke.BRL.MIL (Doug Gwyn) (04/27/89)
In article <1043@itivax.iti.org> scs@vax3.iti.org (Steve Simmons) writes: > a = if ( a == 1 ) 12 ; else 14 ; >Anybody else ever use stuff like this? How could they, since it was never valid C? Why do you think there had to be a ?: operator if the above was supposed to work? If you had a compiler that accepted the above code and produced the answer you expected, it was due to some fortuitous combination of factors specific to that particular compiler, including lack of diagnostic ability.
henry@utzoo.uucp (Henry Spencer) (04/27/89)
To address the Subject line first, a statement is NEVER an expression in C. In article <1043@itivax.iti.org> scs@vax3.iti.org (Steve Simmons) writes: > a = if ( a == 1 ) > 12 ; > else > 14 ; This is an interesting construct, but it's not legal C. Probably some compiler writer's bright idea. >We tried it out on all the C compilers we could find (BSD 4.3, Gould, >UNIX-PC, gcc) and it fails. But the error messages are quite cryptic >(we like gcc: "parse error after 'a'") and largely don't address >the real problem... When you feed a compiler something that it considers gibberish, it's fairly normal for the error messages to be a bit cryptic. -- Mars in 1980s: USSR, 2 tries, | Henry Spencer at U of Toronto Zoology 2 failures; USA, 0 tries. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
peter@ficc.uu.net (Peter da Silva) (04/28/89)
In article <1043@itivax.iti.org>, scs@vax3.iti.org (Steve Simmons) writes: > a = if ( a == 1 ) > 12 ; > else > 14 ; I modified a C compiler to do that once. It's very BCPL/Algol-ish. Rather neat, too. Pity it's not 'C'. -- Peter da Silva, Xenix Support, Ferranti International Controls Corporation. Business: uunet.uu.net!ficc!peter, peter@ficc.uu.net, +1 713 274 5180. Personal: ...!texbell!sugar!peter, peter@sugar.hackercorp.com.
sho@pur-phy (Sho Kuwamoto) (04/28/89)
In article <1043@itivax.iti.org> scs@vax3.iti.org (Steve Simmons) writes:
<
<A friend was over tonight and we were talking over wierd C code
<we have written. [...]
<
<main()
<{
< int a = 0 ;
<
< a = if ( a == 1 )
< 12 ;
< else
< 14 ;
< printf( "Value of a is %d\n", a ) ;
<}
Perhaps someone else can explain this more technically, but it seems
to me that...
1) "if" is a branching operation. It cannot be assigned to an lvalue.
2) "12;" cannot be executed. It is not a legal expression.
This, however, should work:
a = (a==1)?12:14;
-Sho
laba-4he@web-4a.berkeley.edu (The Cybermat Rider) (04/28/89)
In article <1127@ptolemy.arc.nasa.gov> raymond@ptolemy.arc.nasa.gov (Eric A. Raymond) writes: >In article <1043@itivax.iti.org> scs@vax3.iti.org (Steve Simmons) writes: >> a = if ( a == 1 ) >> 12 ; >> else >> 14 ; [....] >You can accomplish this behavior via the ?: connstruct: > > a = (a ? 12 : 14); ^^^^^^^^^^^^^^^^^^ Close, but not quite -- if a is 2, you'll get *12*, not 14. It should be: a = ((a == 1) ? 12 : 14); > >Incidently, I beleive the comma operator allows you to approach a >progn (or is it prog1?): > > a = (x=1, y=2, z=3); > >A is 3 if the last expr is returned (progn-like), otherwise 1 >(prog1-like). Look it up. I did. Sorry, but a is guaranteed to be 3. To quote K&R 2: A7.18 Comma Operator expression: assignment-expression expression , assignment-expression A pair of expressions separated by a comma is evaluated left-to-right, and the value of the left expression is discarded. [.....] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >-- >Eric A. Raymond (raymond@ptolemy.arc.nasa.gov) >G7 C7 G7 G#7 G7 G+13 C7 GM7 Am7 Bm7 Bd7 Am7 C7 Do13 G7 C7 G7 D+13: Elmore James ---------------------------------------------------------------------------- Adrian Ho a.k.a. The Cybermat Rider University of California, Berkeley laba-4he@web.berkeley.edu (WEB Evans, Home of The CS Freakies) Disclaimer: Nobody takes me seriously, so is it really necessary?
dg@lakart.UUCP (David Goodenough) (04/28/89)
scs@vax3.iti.org (Steve Simmons) sez: > ...... > we wrote code like: > > main() > { > int a = 0 ; > > a = if ( a == 1 ) > 12 ; > else > 14 ; > printf( "Value of a is %d\n", a ) ; > } UUMMPHFF!!!! When did C compilers learn to talk ALGOL. Either that or it's a funky way of doing: a = (a == 1) ? 12 : 14; from a somewhat odd compiler. > We tried it out on all the C compilers we could find (BSD 4.3, Gould, > UNIX-PC, gcc) and it fails. I'm not in the least surprised - C was never specified to do that. > But the error messages are quite cryptic > (we like gcc: "parse error after 'a'") and largely don't address > the real problem. Basically the syntax is totally out to lunch, and different parsers will decide that different errors exist. > Anybody else ever use stuff like this? Not a chance - it's not legal C. -- dg@lakart.UUCP - David Goodenough +---+ IHS | +-+-+ ....... !harvard!xait!lakart!dg +-+-+ | AKA: dg%lakart.uucp@xait.xerox.com +---+
gwyn@smoke.BRL.MIL (Doug Gwyn) (04/29/89)
>2) "12;" cannot be executed. It is not a legal expression.
"12" is a legal expression, and "12;" is therefore a legal
(but not very useful) expression-statement.
geoff@cs.warwick.ac.uk (Geoff Rimmer) (04/29/89)
In article <2208@pur-phy> sho@pur-phy (Sho Kuwamoto) writes: > In article <1043@itivax.iti.org> scs@vax3.iti.org (Steve Simmons) writes: > < a = if ( a == 1 ) > < 12 ; > < else > < 14 ; > > 2) "12;" cannot be executed. It is not a legal expression. '12' is an expression. '12;' is a statement. Anything that is a valid expression (such as '12') can always be made into a valid statement by adding a ';'. OK, so it might not *do* anything to have a statement "12;" but it is syntactically correct C. > -Sho Geoff /---------------------------------------------------------------\ | GEOFF RIMMER - Friend of fax booths, ANSI C, PCBH, | | phone *numbers* & MPFC & printf | | email : geoff@uk.ac.warwick.emerald | | address : Computer Science Dept, Warwick University, | | Coventry, England. | | PHONE : +44 203 692320 (10 lines) If I'm out please | | leave a message with my secretary. | | FAX : +44 865 726753 | \---------------------------------------------------------------/
jeremyr@cs.qmc.ac.uk (Jeremy Roussak) (04/29/89)
In article <4011@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes: >In article <1043@itivax.iti.org>, scs@vax3.iti.org (Steve Simmons) writes: >> a = if ( a == 1 ) >> 12 ; >> else >> 14 ; > >I modified a C compiler to do that once. It's very BCPL/Algol-ish. Rather >neat, too. Pity it's not 'C'. >-- >Peter da Silva, Xenix Support, Ferranti International Controls Corporation. > >Business: uunet.uu.net!ficc!peter, peter@ficc.uu.net, +1 713 274 5180. >Personal: ...!texbell!sugar!peter, peter@sugar.hackercorp.com. It may be very Algol-ish, but it's certainly not BCPL. BCPL has a==0 -> 10, 12 It doesn't use if in that way, unles you include the valof/resultis clause. Jeremy Roussak Just a part-time hacker
tony@oha.UUCP (Tony Olekshy) (05/06/89)
In <1043@itivax.iti.org>, Steve Simmons (scs@vax3.iti.org) writes... > > main() { int a = 0 ; a = if ( a == 1 ) 12 ; else 14 ; } > > We tried it out on all the C compilers we could find (BSD 4.3, Gould, > UNIX-PC, gcc) and it fails. But the error messages are quite cryptic... Well, well, well, if anyone praised the Xenix C compiler I would be the first to provide lists of bugs, but I tried the above (too tired to party) and cc said: @test.c(1) : error 59: syntax error : 'if' @test.c(1) : error 59: syntax error : 'constant' @test.c(1) : error 59: syntax error : 'else' Sounds right to me. Yours, etc., Tony Olekshy (...!alberta!oha!tony or tony@oha.UUCP).