Operators, order, relevance, read and priority

5

I came across the following question in a comment of the following

  

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

So I decided to create an answer by explaining right away to take questions or teach those who do not know.

    
asked by anonymous 12.11.2014 / 14:27

3 answers

5

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

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

Precedence 1 (evaluated first, from left to right):

  • var++ : Postfix increment.
  • var-- : Postfixed decrease.
  • func(param) : Function call.
  • array[indice] : Access to indexes of an array.
  • var.membro : Access to members of union s or struct s.
  • var->membro : Access to members of union s or struct s through pointers.
  • '(type) {list}: Compound literal (C99 or higher)

Precedence 2 (evaluated from right to left):

  • ++var : Prefixed increment.
  • --var : Predefined decrease.
  • +var : Positive unary sign. In practice it does nothing.
  • -var : Unary negative sign. Reverses the operand signal.
  • !var : Non-logical operator.
  • ~var : Operator NOT bitwise.
  • (tipo) var : Cast types.
  • *var : Indirect pointers.
  • &var : Address Operator.
  • sizeof var : Size operator 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 division.

Precedence 4 (evaluated from left to right):

  • a + b : Sum.
  • a - b : Subtraction.

Precedence 5 (evaluated from left to right):

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

Precedence 6 (evaluated from left to right):

  • a > b : Higher-than.
  • a < b : Less-than.
  • a >= b : Higher-or-equal.
  • a <= b : Minor-or-equal.

Precedence 7 (evaluated from left to right):

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

Precedence 8 (evaluated from left to right):

  • a & b : Operator E bitwise.

Precedence 9 (evaluated from left to right):

  • a ^ b : Operator OR EXCLUSIVE bitwise.

Precedence 10 (evaluated from left to right):

  • a | b : Operator OR bitwise.

Precedence 11 (evaluated from left to right):

  • a && b : Operator E logical.

Precedence 12 (evaluated from left to right):

  • a || b : Operator OR logical.

Precedence 13 (evaluated from right to left):

  • a ? b : c : Conditional ternary.

Precedence 14 (evaluated from right to left):

  • a = b : Attribution.
  • a += b : Assignment with sum.
  • a -= b : Assignment with subtraction.
  • a *= b : Assignment with multiplication.
  • a /= b : Assignment with division.
  • a %= b : Assignment with remainder of division.
  • a <<= b : Assignment with bit shift to the left.
  • a >>= b : Assignment with bit shift to the right.
  • a &= b : Allocation with logical E.
  • a ^= b : Assignment with logical EXCLUSIVE OU.
  • a |= b : Allocation with logical OR.

Precedence 15 (evaluated last, from left to right):

  • a, b : Separation of values, results in the last evaluated value.
12.11.2014 / 14:54
2

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

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

!= is read as different . As the boolean operator ! means not , that is, it inverts the boolean result, it was considered good to represent the different as "not equal", ie a "! ==", simplifying != .

So in any case the != can be confused with < that asks if one value is smaller than the other.

Of course, if a value is smaller it is also different, but the reverse is not true. A value can be different being both smaller and greater than the other value compared. Different means total difference. Smaller is a difference in only one direction.

Note that there is no precedence between relational operators. Whatever comes first from left to right will be executed. Except the == and != that have lower precedence.

See the Wikipedia article on the subject .

    
12.11.2014 / 14:54
1

Logical unary operators

Logical unary operators are those that 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 is true, or 1, the negation operator reverses the result, ie! == 1 results in false or 0.

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

Arithmetic unary operators

  • ( + ) and ( - ) the plus and minus unaries represents the sign of a number, variable, or expression. example +5 -3 = +2

Remembering that the default is (+).

Arithmetic Binary Operators

Arithmetic binary 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, executes the operation of mathematical congruence, 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 the 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 one number, variable, or expression is greater than the other. 5> 3 true

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

- (> =) same as above but if equal 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;
}
  • ( 3 true

int a = 5, b = 3;     if b

- (< =) same as above but if equal 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 equality comparator, if that equals that.

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

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

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

Logical Operators & amp; it is logical || or logical

table of priorities, origin and associativity

    
12.11.2014 / 16:09