Блог пользователя MciprianM

Автор MciprianM, 13 лет назад, По-английски
Long time no hear!
Now I have a question, whose answer I don't know.

Take the following piece of code:

n>?=m;

How does it work? And why? Does this work in C? Or only in C++? Or viceversa? Or both?
Does the question mark have any connection with the ternary operator ( ?: ) and/or operator precedence ?
If you know the answer please leave a comment.

I have found something new on the matter, now that I have received some comments: see link

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
It's equivalent to n=max(n,m) (or min, not sure), and works only with gcc.
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
It's equivalent to n = max(n, m), but n is evaluated only once. It's a GCC extension.
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
I understand that it gets the maximum, but how does it work? I mean is >?= an operator or are these three different operators and if it is what does the ? do ? Could you explain a little bit more?
  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    It's whole operator which exists only in GCC. Unfortunately, it is unavailable since GCC4.
13 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится
How do you like this equivalent?
(m>n) && n=m;
  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    Nice :)
    But it's still longer than even n = max(n, m);
  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    I have thought of this kind of statements while I was working on one of my php assignments. There is the famous structure: mysql_connect() or die(). What other useful constructs do you know, that use this logical operators peculiarity ( in and if first is false second is not evaluated, in or if first is true second is not evaluated)? And is this implementation of the && operator in the standard? Or are there implementations in which both operands of && are evaluated, no matter the truth value of the first?
  • 13 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
    nice!! but how does
    (m>n)&&n=m 
    work??
    • 13 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится
      The compiler has this implementation thing of the && operator. If the left operand evaluates to false then it knows that the whole expression is false no matter what the second operator evaluates to, so it doesn't evaluate the second operand. In this case, if m is not greater than n( or n is smaller or equal than m ) then the assignment is not performed. The assignment is performed only if m>n which is equivalent to
      if ( m > n )
               n = m;