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.
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.
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:
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 union
s or struct
s.var->membro
: Access to members of union
s or struct
s through pointers.++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).a * b
: Multiplication.a / b
: Division quotient.a % b
: Rest of the division.a + b
: Summing up.a - b
: Subtraction.a << b
: Bit shift to the left.a >> b
: Bit offset to the right.a > b
: Greater-than.a < b
: Minor.a >= b
: Greater-or-equal.a <= b
: Lesser-or-equal.a == b
: Equal.a != b
: Different.a & b
: Operator AND bitwise.a ^ b
: Operator OR BITWISE EXCLUSIVE.a | b
: Operator OR bitwise.a && b
: Operator AND logical.a || b
: Operator OR LOGICAL.a ? b : c
: Ternary parole.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.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:
Remembering that 0 is how C interprets the false, and 1 the true, are synonymous.
Arithmetic unary operators
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.
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;
}
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
Browser other questions tagged c operators
You are not signed in. Login or sign up in order to post.
(!=) == (<); //true
(!=) === (<); //false
– Antony Alkmim
@Antonyalkmim ?!?!?!
– Maniero
@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.– Antony Alkmim
The second part of the comment quoted in this question is about something else, it was said because his code was very disorganized.
– Maniero
I’m answering you back =/
– Joannis
Do you think any answer deserves acceptance?
– Maniero