On 7/17/19 9:55 AM, David Crayford wrote:
On 2019-07-16 4:44 AM, Tom Marchant 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.
It's not just C it's just about every programming language invented in
the last 30 years. Of the languages I use I can list them:
C/C++, Java, JavaScript, Python, Lua, Ruby, Rust etc, etc.
Once you get used to it it's second nature. I get confused now when I
code REXX with all the superfluous operators it has like <>, />. /< etc.
FYI, Python does not let you do 'if foo = bar'. It's a syntax error. But
the newest Python, 3.8, will allow you to do 'if (foo := bar)' . Note
the colon and parenthesis.
https://www.python.org/dev/peps/pep-0572/
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN