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.

-- 
Tom Marchant

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to