*Some* compilers generate a warning. if (foo = bar) ... is almost certainly an error, and if it is not, you can code it as if ((foo = bar) != 0) ... or several other ways.
Charles -----Original Message----- From: IBM Mainframe Discussion List [mailto:IBM-MAIN@LISTSERV.UA.EDU] On Behalf Of Tom Marchant Sent: Monday, July 15, 2019 1:44 PM To: IBM-MAIN@LISTSERV.UA.EDU Subject: Re: Where put the notional constant in a condition (Was RE: JCL COND Parameter) On Mon, 15 Jul 2019 13:30:51 -0700, Charles Mills wrote: >Some C programmers are fond of if (7 == foo) rather than the more conventional >if (foo == 7) because if one gets in the habit of doing so and then >accidentally codes if (7 = foo) one gets a compile error rather than >unexpected behavior. > >For those not familiar with C, foo == 7 is a relational expression, foo = 7 is >an assignment, and if (foo = 7) ... compiles as though one had coded > >foo = 7; if (foo != 0) /* which will be true of course */ ... > >which is not at all what was presumably intended. > >7 = foo is always a compile-time error; you can't assign a variable to a >constant. It's one of the things that I don't like about C. While you can code if 7 = foo and the compiler will catch your error, there is nothing you can do to protect yourself against the mistake of if foo = bar. ---------------------------------------------------------------------- For IBM-MAIN subscribe / signoff / archive access instructions, send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN