if (nr = 0x56) ...
That took and entire day to find and made me religiously avoid lvalue assigments in expressions by doing: if (0x56 == nr) ... if (nr = 0x56) ...
That took and entire day to find and made me religiously avoid lvalue assigments in expressions by doing: if (0x56 == nr) ...It's also mostly useless because your compiler should warn you about your first construct (and tell you to add a layer of parens if that's what you really want to do).
I realize I'm being a bit irrational with this whole thing but for some reason code like this drives me nuts (that and people not putting space around arithmetical operators and such: int a=2*4+foo(1,2,3)).
"But why don't you use better languages, then?" Because in many cases (low-level resource-constrained systems programming), it's much, much easier to deal with these weird gotchas than to attempt to get exotic [1] languages such as Python, Ruby, Java, and sometimes even C++ (heap? who needs a heap??), to work. Even in cases where you're able to get these languages to work easily, you're just forcing yourself to become intimately familiar with the internals of some language with which you'll ultimately wind up wrestling [2]. Normally the point of such languages is to abstract away the system. This is the exact opposite of what you need in these environments.
"But it annoys me when people use literal lvalues where they don't need to." Ah, but this is the rub of forming a habit.
1: I'm mostly sarcastic in my use of the word "exotic" here. Don't go crazy, now. ;-)
2: Though to totally contradict myself, I will say that both Rust and Go look very, very interesting here.
Anyho, it really depends on what your -W level is set to. It's good practice to use "-Wall"
However, a compound conditional can actually be more clear if the assignment is there in many cases. For example:
if ( (elt = db.getNextResult) && elt.shouldProcess){
... ...
}
In a compound conditional, the assignment is forced to be wrapped in parenthesis, so the ambiguity doesn't exist.Anyho, that's that.
except where noted [e.g. special rules for && and ||], the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is Unspecified.
This is basic stuff Chris, let's not debate the obvious, this kind of code is all over the place in the Linux kernel.
* http://linux-kernel.2935.n7.nabble.com/PATCH-drivers-net-ifb...
* http://linux-kernel.2935.n7.nabble.com/PATCH-1-4-silicom-che...
* http://linux-kernel.2935.n7.nabble.com/PATCH-0-4-Staging-sil...
Not to mention, checkpatch.pl checks for assignments in if statements:
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.g...
I should point out that someone tried to add a backdoor into the Linux kernel source through the use of an assignment in an if statement in 2004. What are your thoughts on this?
https://freedom-to-tinker.com/blog/felten/the-linux-backdoor...
Also, where is the place in the C99 standard that details the exception? Genuinely curious. When I reviewed the standard, all that is said about the if statement is in section 6.8.4.1, and that says nothing about short circuited logic, certainly nothing about "exceptions to the rule". This seems compiler specific, but I'm happy to be shown to be wrong - if you can point me to the part in the standard.
The construct is a useful idiom, but oftentimes there would be or ought to be a local coding standard recommending that you make what you're doing explicit by comparing the assignment result to the appropriate flavour of zero:
if ((foo = bar) != NULL)
Now everybody can see what's going on, the warning doesn't arise, and no pedants can complain about the theoretically-unnecesary parentheses in `if ((foo = bar))`.In my case, at least one of the compilers in question was for the 8051, the platform that won't fucking die. Due to the peculiarities of the aforementioned piece of silicon crap, the compiler was not entirely ANSI-compliant (various #pragmas were actually required to produce working code, indicating various parameters related to memory organisation and the such), thus making it rather difficult to compile the codebase with anything but that particular compiler.
> The construct is a useful idiom, but oftentimes there would be or ought to be a local coding standard recommending that you make what you're doing explicit by comparing the assignment result to the appropriate flavour of zero
This is the solution I adhere to as well.
Also when you're drunk, the warnings result in "ahh who cares" :)
[0] http://www.codinghorror.com/blog/2012/07/new-programming-jar...