Operator Types
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, >, < , >=, < =
- Logical Operators: &&, ||, !
- Bitwise Operators: &, |, ^, ~, < < , >>
- Assignment Operators: =, +=, -=, *=, /=, %=
- Increment/Decrement Operators: ++, —
C operators are symbols that perform operations on variables and values. They are essential for performing arithmetic, logical, comparative, and many other operations.
C operators are categorized in the following categories −
Operator Types
In Programming Languages, the associativity of an operator is a property that determines how operators of same precedence are grouped together in the absence of parenthesis.
Operator | Associativity | Description |
---|---|---|
++, -- | Right to Left | Increment, Decrement |
*, /, % | Left to Right | Multiplication, Division |
+, - | Left to Right | Addition, Subtraction |
==, != | Left to Right | Equality, Inequality |
&& | Left to Right | Logical AND |
= | Right to Left | Assignment |
The assignment operator is used to assign values to variables.
Operator | Example | Meaning | |||
---|---|---|---|---|---|
= | x = y | Assign y to x | |||
+= | x += y | x = x + y | |||
-= | x -= y | x = x - y | |||
*= | x *= y | x = x * y | |||
/= | x /= y | x = x / y | |||
%= | x %= y | x = x % y | |||
&= | x &= y | x = x & y | |||
^= | x ^= y | x = x ^ y | |||
<<= | x <<= y | x = x << y | |||
>>= | x >>= y | x = x >> y |
Operate on individual bits of data.
&
(AND)|
(OR)^
(XOR)~
(NOT)<<
(left shift)>>
(right shift)#include <stdio.h>int main() { int a = 10, b = 5, result; int x = 1, y = 0;
// Arithmetic Operators result = a + b; printf("Addition: %d + %d = %d\n", a, b, result);
result = a - b; printf("Subtraction: %d - %d = %d\n", a, b, result);
result = a * b; printf("Multiplication: %d * %d = %d\n", a, b, result);
result = a / b; printf("Division: %d / %d = %d\n", a, b, result);
result = a % b; printf("Modulus: %d %% %d = %d\n", a, b, result);
// Relational Operators printf("a > b: %d\n", a > b); printf("a < b: %d\n", a < b); printf("a == b: %d\n", a == b); printf("a != b: %d\n", a != b);
// Logical Operators printf("x && y: %d\n", x && y); // AND printf("x || y: %d\n", x || y); // OR printf("!x: %d\n", !x); // NOT
// Assignment Operators result = a; // Assignment result += 5; // result = result + 5 printf("Result after += 5: %d\n", result);
result *= 2; // result = result * 2 printf("Result after *= 2: %d\n", result);
return 0;}
Addition: 10 + 5 = 15Subtraction: 10 - 5 = 5Multiplication: 10 * 5 = 50Division: 10 / 5 = 2Modulus: 10 % 5 = 0a > b: 1a < b: 0a == b: 0a != b: 1x && y: 0x || y: 1!x: 0Result after += 5: 15Result after *= 2: 30