Operator in C

An operator is a symbol that performs a specific operation on one or more operands (variables or values). Operators are used to manipulate data and variables in a program.

Classification of Operators

Operators in C are classified into the following categories:


1. Arithmetic Operators

Perform basic arithmetic operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b

2. Relational Operators

Compare two values and return a boolean result (true or false).

Operator Description Example
== Equal to a == b
!= Not equal to a != b
< Less than a < b
> Greater than a > b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b

3. Logical Operators

Used to perform logical operations.

Operator Description Example
&& Logical AND a && b
|| Logical OR  a || b
! Logical NOT !a

4. Bitwise Operators

Perform operations at the bit level.

Operator Description Example
& Bitwise AND a & b
| Bitwise OR  a|b
^ Bitwise XOR a ^ b
~ Bitwise Complement ~a
<< Left Shift a << 2
>> Right Shift a >> 2
5. Assignment Operators

Assign values to variables.

Operator Description Example
= Assign a = 5
+= Add and assign a += 5
-= Subtract and assign a -= 5
*= Multiply and assign a *= 5
/= Divide and assign a /= 5
%= Modulus and assign a %= 5
6. Increment and Decrement Operators

Increase or decrease the value of a variable by 1.

Operator Description Example
++ Increment a++ (Post), ++a (Pre)
-- Decrement a-- (Post), --a (Pre)
7. Conditional (Ternary) Operator

A shorthand for if-else conditions.

Operator Description Example
? : Condition ? True : False a > b ? a : b
8. Special Operators

Used for specific purposes.

Operator Description Example
sizeof Returns the size of a data type sizeof(int)
& Address of a variable &a
* Pointer to a variable *ptr
Example:

#include <stdio.h>

int main() {
int a = 10, b = 5, result;

// Arithmetic Operators
result = a + b; // Addition
printf(“Addition: %d\n”, result);

// Relational Operators
printf(“a > b: %d\n”, a > b); // Greater than

// Logical Operators
printf(“(a > b AND a != 0)= %d\n”, (a > b) && (a != 0)); // Logical AND

// Assignment Operators
a += b; // a = a + b
printf(“After a += b: %d\n”, a);

return 0;
}

Output:
  1. Arithmetic Operators (a + b)
    • a=10, b=5
    • result = a + b = 10 + 5 = 15
    • Output: Addition: 15
  2. Relational Operators (a > b)
    • a=10, b=5
    • 10>5 is true, so output is 1.
    • Output: a > b: 1
  3. Logical Operators ((a > b) && (a != 0))
    • a=10, b=5
    • (10>5) AND (10≠0)
    • Both conditions are true, so output is 1.
    • Output: (a > b AND a != 0) = 1
  4. Assignment Operators (a += b)
    • a=10, b=5
    • a = a + b = 10 + 5 = 15
    • Output: After a += b: 15

 

Leave a Comment

Your email address will not be published. Required fields are marked *