Do the ||, &&, ! operators have a specific order in which the compiler reads them? If so what is it?
Do the ||, &&, ! operators have a specific order in which the compiler reads them? If so what is it?
I believe they have the same precedence, so the compiler will read them left to right.
Any one else know for sure.
The answeres in my C book somwhere but you shouldn't really need it. Just keep everything separated into brackets then you can be sure of whats being evaluated when.
The "!" (NOT) operator has higher precedence than the logical AND and OR operators. AND and OR have the same precedence and are evaluated left to right, (although some would argue that && is higher than ||).
Whatever, it is poor programming practice to rely on operator precedence.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
liberal use of parentheses and proper coding style means that you can all but forget about operator precedence.This is a feature of c++ that causes more problems than it fixes.
for instance this looks good....
while ('\n' != *dest++ = *source++);
but i would rather see this anyday....
while (1)
{
*dest=*source;
dest++;
source++;
if(*(dest-1)=='\n') break;
}
both do exactly the same thing.But to me and many others it is much more obvious what is going on in the second example and with use of parenthesis you do not need worry about operator precedence.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
The best precedence advice I can give is this.
Multiplication and division come before addition and subtraction. Everything else should be in parentheses.
Btw: && || evaluate left to right, set up your tests so that the most critical test is on the left and evaluation will stop if it returns false.
-Prelude
My best code is written with the delete key.
>>> while ('\n' != *dest++ = *source++);
Exactly SC. In some places, these incredibly terse, "efficient" versions are applauded. These people have obviously never been in industry! As for efficiency, I would imagine any half decent compiler would reduce the two examples you gave to the same object!
Readability and maintainability is the key concepts.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.