C Programming
Operators

Operators in C

Operators are special symbols in C that perform operations on variables and values. C provides a rich set of operators to handle arithmetic, logical, bitwise, and other operations.

1. Types of Operators

C operators can be classified into several categories based on their functionality:

a. Arithmetic Operators

These operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (Remainder)a % b

Example:

int a = 10, b = 3;
int sum = a + b;       // 13
int diff = a - b;      // 7
int product = a * b;   // 30
int quotient = a / b;  // 3
int remainder = a % b; // 1

b. Relational Operators

These operators are used to compare two values.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equala >= b
<=Less than or equala <= b

Example:

if (a > b) {
    printf("a is greater than b");
}

c. Logical Operators

Logical operators are used to combine multiple conditions.

OperatorDescriptionExample
&&Logical AND(a > b && b > c)
||Logical OR(a > b || b > c)
!Logical NOT!(a > b)

Example:

if (a > b && b > c) {
    printf("Both conditions are true");
}

d. Bitwise Operators

These operators perform operations on bits and are used for low-level programming.

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left Shifta << 1
>>Right Shifta >> 1

Example:

int x = 5;  // (Binary: 0101)
int y = 3;  // (Binary: 0011)
int andResult = x & y; // (Binary: 0001) => 1
int orResult = x | y;  // (Binary: 0111) => 7

e. Assignment Operators

These operators are used to assign values to variables.

OperatorDescriptionExample
=Simple assignmenta = b
+=Add and assigna += b (same as a = a + b)
-=Subtract and assigna -= b (same as a = a - b)
*=Multiply and assigna *= b (same as a = a * b)
/=Divide and assigna /= b (same as a = a / b)
%=Modulus and assigna %= b (same as a = a % b)

Example:

int a = 5;
a += 3; // a becomes 8

f. Unary Operators

Unary operators operate on a single operand.

OperatorDescriptionExample
++Increment++a or a++
--Decrement--a or a--
-Unary minus (negation)-a
&Address-of operator&a
*Dereference operator*ptr

Example:

int a = 5;
int b = ++a; // b is 6, a is also 6

g. Ternary Operator

The ternary operator is a shorthand for the if-else statement.

Syntax:

condition ? expression1 : expression2;

Example:

int max = (a > b) ? a : b; // If a is greater than b, max is a; otherwise, max is b.

Diagram of Operators in C:


Summary of Operators in C

Operators are fundamental to C programming, allowing you to perform various operations on data. Understanding the different types of operators and their uses is crucial for effective programming.

Key Takeaways:

  • Arithmetic Operators perform mathematical calculations.
  • Relational Operators compare values.
  • Logical Operators combine conditions.
  • Bitwise Operators manipulate bits.
  • Assignment Operators assign values to variables.
  • Unary Operators operate on a single operand.
  • Ternary Operator provides a shorthand for if-else.