Operator is a symbol that is used to perform mathematical operations.
|
|
|
|
Assignment |
= |
|
Arithmetic |
+,-,*,/,% |
|
Logical |
&&, ||, ! |
|
Relational |
<, >, <=, >=, ==, != |
|
Shorthand |
+=, -=, *=, /=, %= |
|
Unary |
++, -- |
|
Conditional |
() ? : ; |
|
Bitwise |
&, |, ^, <<, >>, ~ |
main(){
int a=21;
int b=10;
int c;
c = a+b; // c is 31
c = a-b; // c is 11
c = a*b; // c is 210
c = a/b; // c is 2
c = a%b; // c is 1
c = a++; // c is 21
c = a--; // c is 22
}
Bitwise operator works on bits and perform bit by bit operation.
Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
Then A & B = 0000 1100 //and
A|B = 0011 1101 //or
A^B = 0011 0001 //xor
~A = 1100 0011 //complement
main(){ unsigned int a = 60; /* 60 = 0011 1100 */ unsigned int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ c = a | b; /* 61 = 0011 1101 */ c = a ^ b; /* 49 = 0011 0001 */ c = ~a; /* -61 = 1100 0011 */ c = a << 2; /* 240 = 1111 0000 */ c = a >> 2; /* 15 = 0000 1111 */ }
main(){ int a,b; a = 10; b = ( a == 1) ? 20 : 30; printf("Value of b is %d/n", b); b = ( a == 10) ? 20 : 30; printf("Value of b is %d/n", b); }
Value pf b is 30
Value of b is 20
Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:
For example x = 7 + 3 * 2; Here x is signed 13, not 20 because opeator * has higher precedence than '+'' so it first get multiplied with 3 * 2 and then adds into 7.
here operator with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. within an expression, higher precedence operators will be evaluated first.
Category |
Operator |
Associativity |
|
Postfix |
() [] -> . ++ -- |
Left to Right |
Unary |
+ - ! ~ ++ -- (type) * & sizeof |
Right to Left |
Multiplicative |
* / % |
Left to Right |
Additive |
+ - |
Left to Right |
Shift |
<< >> |
Left to Right |
Relational |
< <= > >= |
Left to Right |
Equality |
== != |
Left to Right |
Bitwise AND |
& |
Left to Right |
Bitwise XOR |
^ |
Left to Right |
Bitwise OR |
| |
Left to Right |
Logical AND |
&& |
Left to Right |
Logical OR |
|| |
Left to Right |
Conditional |
?: |
Right to Left |
Assignment |
= += -= *= /= %= >>= <<= &= ^= |= |
Right to Left |
Comma |
, |
Left to Right |