Operators, order, relevance, how it is read and priority

Asked

Viewed 937 times

5

I came across the following question in a comment from the following Question

One more question. The != operator is the same thing as < ?

So I decided to come up with an answer by explaining it right away.

  • (!=) == (<); //true (!=) === (<); //false

  • 4

    @Antonyalkmim ?!?!?!

  • @bigown did not understand my comment! I meant that actually != is not the same thing as <, but depending on what you do... Example: for X smaller 3, all values will be different from 3. But for X different from 3, the values may be both higher and smaller than 3.

  • The second part of the comment quoted in this question is about something else, it was said because his code was very disorganized.

  • I’m answering you back =/

  • Do you think any answer deserves acceptance?

Show 1 more comment

3 answers

5


The operator != is not the same thing as <. The operator != means inequality, while the < means smaller-than.

According to this here, this is the precedence of operators:

Precedence 1 (evaluated first, left to right):

  • var++: Post-fixed increment.
  • var--: Post-fixed decrease.
  • func(param): Function call.
  • array[indice]: Access to indexes of an array.
  • var.membro: Access to members of unions or structs.
  • var->membro: Access to members of unions or structs through pointers.
  • `(type){list}: Compound literal (C99 or higher)

Precedence 2 (evaluated from right to left):

  • ++var: Preset increment.
  • --var: Predetermined decrease.
  • +var: Sign of positive anointing. In practice does nothing.
  • -var: Unary negative sign. Reverse operand signal.
  • !var: NON-LOGICAL OPERATOR.
  • ~var: NON-BITWISE OPERATOR.
  • (tipo) var: Type cast.
  • *var: Pointer indirect.
  • &var: Address operator.
  • sizeof var: Operator size in memory.
  • _Alignof var: Alignment operator (C11 or higher).

Precedence 3 (evaluated from left to right):

  • a * b: Multiplication.
  • a / b: Division quotient.
  • a % b: Rest of the division.

Precedence 4 (evaluated from left to right):

  • a + b: Summing up.
  • a - b: Subtraction.

Precedence 5 (evaluated from left to right):

  • a << b: Bit shift to the left.
  • a >> b: Bit offset to the right.

Precedence 6 (evaluated from left to right):

  • a > b: Greater-than.
  • a < b: Minor.
  • a >= b: Greater-or-equal.
  • a <= b: Lesser-or-equal.

Precedence 7 (evaluated from left to right):

  • a == b: Equal.
  • a != b: Different.

Precedence 8 (evaluated from left to right):

  • a & b: Operator AND bitwise.

Precedence 9 (evaluated from left to right):

  • a ^ b: Operator OR BITWISE EXCLUSIVE.

Precedence 10 (evaluated from left to right):

  • a | b: Operator OR bitwise.

Precedence 11 (evaluated from left to right):

  • a && b: Operator AND logical.

Precedence 12 (evaluated from left to right):

  • a || b: Operator OR LOGICAL.

Precedence 13 (evaluated from right to left):

  • a ? b : c: Ternary parole.

Precedence 14 (evaluated from right to left):

  • a = b: Attribution.
  • a += b: Sum assignment.
  • a -= b: Subtraction assignment.
  • a *= b: Multiplication assignment.
  • a /= b: Division assignment.
  • a %= b: Assignment with division rest.
  • a <<= b: Offset bit assignment to the left.
  • a >>= b: Offset bit assignment to the right.
  • a &= b: Allocation with logical E.
  • a ^= b: Allocation with OR LOGICAL EXCLUSIVE.
  • a |= b: Attribution with OR logical.

Precedence 15 (last evaluated from left to right):

  • a, b: Separation of values results in the last evaluated value.

2

Each operator is different from the other. It would make no sense to have operators doing the same thing.

In this case we are talking about relational operators that result in boolean values, i.e., operators that only respond with two states, true or false. You use relational operators to establish a relationship between two values. With it you are asking if these values are equal (==), different (!=), the first greater than the second (>), the first smaller than the second (<), greater or equal (>=), equal to or equal to (<=).

!= is read as different. Like the boolean operator ! means not, that is it reverses the boolean result thought to represent the different as "not equal", ie a "! ==" , simplifying !=.

So under no circumstances != can be confused with < that asks if a value is minor that the other.

Of course, if a value is lower it is different too, but the opposite is not true. A value can be different being smaller and larger than the other value compared. Different means total difference. Smaller is a difference in only one sense.

Note that there is no precedence among relational operators. Whichever comes first from left to right will be executed. Except the == and != which have less precedence.

See the Wikipedia article on the subject.

  • x < y != z will be evaluated as (x < y) != z. And in C the x < y can be evaluated as 0 or 1, which can be equal to or different from z. Most of the time this is just for making program overshadowed, but I’ve seen legitimate uses.

  • @Victor it is true, I went to confirm now. Sometimes I confuse languages.

  • Actually there is a yes order between relational. a < b == c > d is assessed as (a < b) == (c > d). Again, this only serves to write obfuscated or at least confusing code. The == and the != are evaluated after the <, >, <= and >=.

1

Unary operators

Unary operators are those who modify only a number, variable or expression, they are:

  • (!) negation, used in front of boolean results, when using a comparison 1==1 we know that it turns out true, or 1, the negation operator reverses the result, ie ! 1==1 results false or 0.

Remembering that 0 is how C interprets the false, and 1 the true, are synonymous.

Arithmetic unary operators

  • (+) and (-) the more and less unary represents the sign of a number, variable or expression. example +5 -3 = +2

Remembering that the default is (+).

Operators Arithmetic binaries

Binary arithmetic operators are those that perform a mathematical operation on an expression with numbers, variables and/or expressions.

  • (*) Multiplicative operator, performs a multiplication between variable numbers or expressions. 3 * 5 = 8.

  • (/) Splitter operator, performs a mathematical division between variable numbers or expressions. 8 / 5 = 3.
  • (%) Module operator, performs the mathematical congruence operation, ie 5%2 it divides 5 by 2 and uses the rest of the division, ie 5%2=1, or 30%9 = 3
  • (+) and (-) plus and minus perform mathematical sum and subtraction example 5 -3 = 2 or 5+3 = 8

Binary Comparators or relational operators

the result of a relational operation is one if it is true or 0 if it is false.

  • (>) represents whether a number, variable, or expression is greater than another. 5>3 true

    int a=5,b=3;
    if(a>b)
    {
       //isso é executado;
    }  
    

- (>=) the same as the one above but if it is the same it results in true

int a=5,b=5;
if(a>b)
{
   //isso não é executado;
}

int a=5,b=5;
if(a>=b)
    {
//isso é executado;
}
  • (<) represents whether a number, variable, or expression is less than another. 5>3 true

int a=5,b=3; if(b

- (<=) the same as the one above but if it is the same it results in true

    int a=5,b=5;
if(b<a)
{
   //isso não é executado;
}

int a=5,b=5;
if(a<=b)
    {
//isso é executado;
}
  • (==) logical equaliser, if this equals that.

    if(5==5) { //this is carried out }

  • (!=) denial of equality, difference. if this is different than that

    if(5!=5) { //this is not executed }

Logical operators && and logical || or logical

priority table, background and associativity inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.