Hi,
It's been probably drummed into everyone since "Hello World" - use a single = sign for assignment and double (==) to test for equality. Is there any case where using a single-equals conditional is valid or makes sense?
A fair amount of Googling failed to turn up the actual behavior or legality of such a statement, only echoes of "be sure to use = for assignment and == for equality". Should if(a=b) return (e.g. "assignment was successful") TRUE always? Or evaluate the assigned value (e.g. return TRUE if b was nonzero)? Or undefined / none of the above?
A quick test with Arduino (avr-gcc) suggests the 2nd case. Is this a standard behavior that can be relied upon, or is this left up to the individual compiler author?
(The more real-world case that got me curious: I want to write a routine that processes the contents of one or more fixed-length buffers that may contain a radio packet (filled by an interrupt routine when a packet is received), and envisioned a "getbuf()" function that would return a pointer to the next buffer containing a packet, or NULL if all are empty. The construction
while(pBuf = getbuf())
{
... process the packet in buffer pointed to by 'pBuf'...
}
seems logical enough, IF it is actually legal C of course
The if doesn't test "if assignment was successful" but whether the expression in brackets is zero, or not.
while(pBuf = getbuf())
Kerthyn is quite correct that this is an often-used idiom. If you could ramp up warnings (which you can't easily) then it would generate this however:
test.cpp:5: warning: suggest parentheses around assignment used as truth value
To "prove" you know what you are doing you would then write this instead:
while((pBuf = getbuf()))
It's not a bad idea, even with warnings turned down, as that shows you are not comparing but assigning (and then testing the result of the assignment).
I think the assignment statement has the same value as the assigned value, so either left or right of the "=" sign. I sometimes use this as a way to test conditions. Say I test key press on a keypad with
while(!(key=keypad.getKey()))
{}
This loop traps while there is no key press and breaks when there is one and it is stored in key for later processing. You CAN do the same but not without longer code. The use of = and == is annoying to some but quite useful if you think we are actually dealing with two concepts, assignment and comparison so it makes sense to use different symbols to avoid ambiguity, which is all programming language is about.
Nick's point about parentheses is a good one. One day, when the Arduino environment allows access to compiler options, I'll do what I do in other environments: set warning level to maximum, treat warnings as errors.
kerthyn:
Nick's point about parentheses is a good one. One day, when the Arduino environment allows access to compiler options, I'll do what I do in other environments: set warning level to maximum, treat warnings as errors.
With c++ and the AVR you might be sorely disappointed as there are some as of yet avr gcc c++ issues that are not
resolved that will create warnings. For example certain uses of PROGMEM attributes.
But if you want to play with compiler and linker options you can do it today.
Have a look at this thread. While you can't set the options in the IDE
It provides a patched 022 IDE that allows setting the options in configuration files so
you can at least modify them without having to rebuild the java code (where all the options are currently set). http://arduino.cc/forum/index.php/topic,54456.0.html]