Operators in C

In C, operators are symbols used to perform specific operations on variables and values. They are a fundamental part of C programming and enable you to execute mathematical, logical, and bitwise computations efficiently.

Types of Operators in C

C operators are broadly categorized as follows:

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

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

Example:

        
#include <stdio.h>
int main()
{
    int a = 10, b = 5;
    
    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);
    printf("a %% b = %d\n", a % b);
    
    return 0;
}
        
        

Output

a + b = 15 a - b = 5 a * b = 50 a / b = 2 a % b = 0

2. Relational Operators

Relational operators compare two values and return a boolean result.

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


Example:


#include <stdio.h>
int main() 
{
    int a = 10, b = 5;
    
    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);
    printf("a >= b: %d\n", a >= b);
    printf("a <= b: %d\n", a <= b);
    
    return 0;
}

Output

a == b: 0 a != b: 1 a > b: 1 a < b: 0 a >= b: 1 a <= b: 0

3. Logical Operators

Logical operators are used to perform logical operations, typically on boolean values.

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

Example:

        
#include <stdio.h>
int main() 
{
  int a = 1, b = 0;
  printf("a && b: %d\n", a && b);
  printf("a || b: %d\n", a || b);
  printf("!a: %d\n", !a);
    
  return 0;
}
        
        

Output

a && b: 0 a || b: 1 !a: 0

4. Bitwise Operators

Bitwise operators are used to perform operations on bits and perform bit-level manipulations.

Operator Description Example
& Bitwise AND a & b
| Bitwise OR a | b
^ Bitwise XOR a ^ b
~ Bitwise NOT ~a
<< Left Shift a << 1
>> Right Shift a >> 1

Example:

        
#include <stdio.h>
int main()
{
  int a = 5, b = 3;
    
  printf("a & b: %d\n", a & b);
  printf("a | b: %d\n", a | b);
  printf("a ^ b: %d\n", a ^ b);
  printf("~a: %d\n", ~a);
  printf("a << 1: %d\n", a << 1);
  printf("a >> 1: %d\n", a >> 1);
    
  return 0;
}
        
        

Output

a & b: 1 a | b: 7 a ^ b: 6 ~a: -6 a << 1: 10 a >> 1: 2

5. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Simple Assignment a = b
+= Add and Assign a += b
-= Subtract and Assign a -= b
*= Multiply and Assign a *= b
/= Divide and Assign a /= b

Example:

        
#include <stdio.h>
int main() 
{
  int a = 10, b = 5;
        
   a += b;
   printf("a += b: %d\n", a);
        
   a -= b;
   printf("a -= b: %d\n", a);
        
   a *= b;
   printf("a *= b: %d\n", a);
        
   a /= b;
   printf("a /= b: %d\n", a);
        
   return 0;
}
        
        

Output

a += b: 15 a -= b: 10 a *= b: 50 a /= b: 10

6. Miscellaneous Operators

Miscellaneous operators are special operators used for specific tasks like size of variables, pointer operations, etc.

Operator Description Example
sizeof Size of data type sizeof(int)
& Address of operator (Pointer) &a
* Dereference operator (Pointer) *ptr

Example:

        
#include <stdio.h>
int main()
{
  int a = 10;
  int *ptr = &a;
    
  printf("Size of int: %zu\n", sizeof(int));
  printf("Address of a: %p\n", &a);
  printf("Value pointed by ptr: %d\n", *ptr);
    
  return 0;
}
        
        

Output

Size of int: 4 Address of a: 0x7ffeb0c29d54 Value pointed by ptr: 10