MciprianM's blog

By MciprianM, 13 years ago, In English
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

  • Vote: I like it
  • +5
  • Vote: I do not like it

13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
It's equivalent to n=max(n,m) (or min, not sure), and works only with gcc.
13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
It's equivalent to n = max(n, m), but n is evaluated only once. It's a GCC extension.
13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    It's whole operator which exists only in GCC. Unfortunately, it is unavailable since GCC4.
13 years ago, # |
  Vote: I like it +7 Vote: I do not like it
How do you like this equivalent?
(m>n) && n=m;
  • 13 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    Nice :)
    But it's still longer than even n = max(n, m);
  • 13 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    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 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it
    nice!! but how does
    (m>n)&&n=m 
    work??
    • 13 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it
      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;