![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0
![]() |
Operator Precedence
Quoting the book Thinking in C++,
while((char c = cin.get()) != 'q') The addition of the extra parentheses would seem like an innocent and useful thing to do, and because you cannot use them, the results are not what you might like. The problem occurs because ‘!=’ has a higher precedence than ‘=’, so the char c ends up containing a bool converted to char. When that’s printed, on many terminals you’ll see a smiley-face character. The author uses while( char c = cin.get() != 'q') to check if the character being input is q and says that extra () can't be used as stated above. Is it ok? AFAIK , if the () are not used , then it is a problem and not otherwise.If not used , cin.get() != 'q' would be evaluated first and the bool value assigned to char |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
I suspect what the author means is:
Quote:
Suffice to say, boolean operators have a higher precedence than the assignment operator, thus: x = y != z x = (y != z) (x = y) != z x + y * z x + (y * z) |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|