# C Operators

C operators are symbols that processes computations or operations o variables and values.
There are classified into several categories

1. Arithmetic Operators - +, -, *, /, %
2. Relational Operators - ==, !=, <, >, <=, >=
3. Logical Operators - &&, !!, !
4. Assignment Operators - =, +=, -=, *=, /=, %/
5. Increment and Decrement Operators - ++, --
6. Bitwise Operators - &, |, ^, ~, <<, >>
7. Conditional (Ternary) Operator - ? :
8. Comma Operator - ,

## Arithmetic Operators

Used for basic mathematical calculations.

Operator

Description

Example

+

int sum = a + b;

-

Subtraction

int diff = a - b;

*

Multiplication

int product = a * b;

/

Division

int quotient = a / b;

%

Modulus (remainder)

int remainder = a % b;

Example
``````#include <stdio.h>

int main() {
int a = 5, b = 2;
int sum = a + b;
int diff = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;

return 0;
}``````

## Relational Operators

Used for comparisons, and they return a boolean result (1 for true, 0 for false).

Operator

Description

Example

==

Equal to

int isEqual = (a == b);

!=

Not equal to

int isNotEqual = (a != b);

>

Greater than

int isGreaterThan = (a > b);

<

Less than

int isLessThan = (a < b);

>=

Greater than or equal to

int isGreaterOrEqual = (a >= b);

<=

Less than or equal to

int isLessOrEqual = (a <= b);

Example
``````#include <stdio.h>

int main() {
int a = 5, b = 2;
int isEqual = (a == b);
int isNotEqual = (a != b);
int isGreaterThan = (a > b);
int isLessThan = (a < b);
int isGreaterOrEqual = (a >= b);
int isLessOrEqual = (a <= b);

return 0;
}``````

## Logical Operators

Used to perform logical operations, often in conditional statements.

Operator

Description

Example

&&

Logical AND

int logicalAnd = (x && y);

||

Logical OR

int logicalOr = (x || y);

!

Logical NOT

int logicalNot = !x;

Example
``````#include <stdio.h>

int main() {
int x = 1, y = 0;
int logicalAnd = (x && y);
int logicalOr = (x || y);
int logicalNot = !x;

return 0;
}``````

## Assignment, Increment and Decrement Operators

char is the only Character type in c.
Assignment Operators are used to assign values to variables.
Increment and Decrement Operators used to increase or decrease the value of a variable by 1.

Operator

Description

Example

=

Assignment

a = 5;

+=

a += 3;

-=

Subtraction assignment

a -= 2;

*=

Multiplication assignment

a *= 4;

/=

Division assignment

a /= 5;

%=

Modulus assignment

a %= 5;;

++

Increment

a++;

--

Decrement

a--;

Example
``````#include <stdio.h>

int main() {
int a = 5;
a += 3;
printf(a);
a -= 2;
printf(a);
a *= 4;
printf(a);
a /= 2;
printf(a);
a %= 3;
printf(a);

printf(a++);
print(a--);

return 0;
}``````

## Bitwise Operators

char is the only Character type in c.
Used for manipulation of individual bits.

Operator

Description

Example

&

Bitwise AND

int bitwiseAnd = a & b;

|

Bitwise OR

int bitwiseAnd = a | b;

^

Bitwise XOR

int bitwiseXor = a ^ b;

~

Bitwise NOT

int bitwiseNot = ~a;

<<

Left shift

int leftShift = a << 1;

>>

Right shift

int rightShift = a >> 1;

Example
``````#include <stdio.h>

int main() {
int a = 5;
int bitwiseAnd = a & b;
int bitwiseOr = a | b;
int bitwiseXor = a ^ b;
int bitwiseNot = ~a;
int leftShift = a << 1;
int rightShift = a >> 1;

return 0;
}``````

## Conditional (Ternary) and Comma Operators

Conditional (Ternary) Operator is a shorthand way to write an if-else statement.
Comma Operator allows multiple expressions to be evaluated in a single statement.

Operator

Description

Example

? :

Ternary conditional (shorthand for if-else statement)

int max = (a > b) ? a : b;

,

Evaluates multiple expressions sequentially

int result = (a++, b++, c++);

The outputs while using the operators majority will be TRUE/FALSE.
Learn about them in C Booleans

# Quick Recap - Topics Covered

Operators in C

## Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.

Example 1
Example 1 Example 2 Example 3 Example 4 Example 5

FEEDBACK