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.
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:
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. ++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). a * b
: Multiplication. a / b
: Division quotient. a % b
: Rest of division. a + b
: Sum. a - b
: Subtraction. a << b
: Bit shift to the left. a >> b
: Bit shift to the right. a > b
: Higher-than. a < b
: Less-than. a >= b
: Higher-or-equal. a <= b
: Minor-or-equal. a == b
: Same. a != b
: Different. a & b
: Operator E bitwise. a ^ b
: Operator OR EXCLUSIVE bitwise. a | b
: Operator OR bitwise. a && b
: Operator E logical. a || b
: Operator OR logical. a ? b : c
: Conditional ternary. 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. a, b
: Separation of values, results in the last evaluated value. 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 .
Logical unary operators
Logical unary operators are those that modify only a number, variable or expression, they are:
Remembering that 0 is how C interprets the false, and 1 the true, are synonymous.
Arithmetic unary operators
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.
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;
}
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