Operators in C

On this page, you will learn about operators in C programming, such as the arithmetic operator, arithmetic operator, relational operator, logical operator assignment operator, bitwise operator, unary operator, and ternary operator. We have given an example for all operators.


What are the Operators in C programming?

  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Assignment Operator
  • Bitwise Operator
  • Unary Operator
  • Ternary Operator

Arithmetic Operator:

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. The most common arithmetic operators in C are +, -, *, /, and %. Lets consider a =20 and b=10 are two variables or operand. See the following table:


Operator Description Example
+ To add to variables a + b is 30
- To substruct from one to another a - b is 10
* Multiplies both operands. a * b is 300
/ Divide both opreand. Note that when you use the division operator with two integers in C, the result will also be an integer. However, if at least one of the operands is a floating point number, then the result will also be in floating point. a / b is 2
% Modulus Opreator is used for find the remainder. a % b is 0

See the following example of arithmetic operator:

  
  #include<stdio.h>
  int main(){
    int a = 20, b = 10;
    printf("A + B = %d\n",a+b);
    printf("A - B = %d\n",a-b);
    printf("A * B = %d\n",a*b);
    printf("15 / 2  = %d\n",15/2.0);
    printf("A % B = %d\n",a%b);
    return 0;
  }
  

Output:

  A + B = 30
  A - B = 10
  A * B = 200
  15 / 2.0 = 7.500000
  A % B = 0                                      

Relational Operators:

Relational operators are used to compare values and determine the relationship between them. They return a Boolean value of either true or false. The most common relational operators in C are ==, !=, < , >, <=, and >=. Lets consider a=20 and b=10 are two variables. See the following table:

Operator Description Example
== Equal to opearator. If two variables are equal to each other, it returns true otherwise false. a == b return false.
!= Not Equal to opearator. If two variables are not equal to each other, it returns true otherwise false. a != b return true
> Greater than operator. If a > b is satisfied, then it returns true; otherwise, it returns false. a > b return true.
< Less than operator. If a < b is satisfied, then it returns true; otherwise, it returns false. a < b return false.
>= Greater than equal to operator. If a > b or a == b is satisfied, then it returns true; otherwise, it returns false. a >= b return ture
<= Less than equal to operator. If a < b or a == b is satisfied, then it returns true; otherwise, it returns false. a <= b return false

See the following example of relational operator:

  
  #include<stdio.h>
  #include<stdbool.h>
  int main(){
    int a = 20, b = 10;
    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 // 0 means false
  A > B = 1 // 1 means true
  A < B = 0 // 0 means false
  A >= B = 1 // 1 means true
  A <= B = 0 // 0 means false                                    

Logical Operators:

Logical operators are used to combine or negate relational expressions. The logical operators in C are && (logical AND), | | (logical OR), and ! (logical NOT). Assume a=1, b=0 and see the following table:

Operator Description Example
&& Its called the logical AND operator. If both the operands are non-zero or true, then the condition becomes true, and if one of them is zero, it comes false. a && b is false
| | Its called Logical OR Operator. If either of the two operands is non-zero, then the condition becomes true. (A | | B) is true
! Its called Logical NOT Operator. It is used to reverse the logical state of its operand. If it is true, then the logical NOT operator will make it false. !(A | | B) is false

See the following example of logical operator:

  
  #include<stdio.h>
  #include<stdbool.h>
  int main(){
    bool 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 // 0 means false
  A || B = 1 // 1 means true
  !A = 0 // 0 means false                                


Assignment Operators:

Assignment operators are used to assign values to variables. They combine an arithmetic or bitwise operation with an assignment. For example, x += 5 is equivalent to x = x + 5. Assume that a = 6,b = 4,c = 0 and see the following table:

Operator Description Example
= This is used to insert a value into a variable. c = a + b so c is 10
+= a += b is same as a = a + b a += b then a is 10
-= a -= b is same as a = a - b a -= b then a is 2
*= a *= b is same as a = a * b a *= b then a is 24
/= a /= b is same as a = a / b a /= b then a is 1.5
%= a %= b is same as a = a % b a %= b then a is 2

See the following example of assignment operator:

  
  #include<stdio.h>
  int main(){
    bool a = 6, b = 4;
    printf("A = %d,a += b);
    return 0;
  }
  

Output:

  A = 10 // because a = a + b                            


Bitwise Operators:

Bitwise operators are used to perform operations on binary numbers. They are used to manipulate individual bits within a byte or a word. The most common bitwise operators in C are &, |, ^, << (left shift), and >> (right shift). See the following table:

Operator Description
& This is used to perform binary AND operation between two or more operands
| This is used to perform binary OR operation between two or more operands
^ This is used to perform binary XOR operation between two or more operands
<< This is used to perform binary left shift operation between two or more operands
>> This is used to perform binary Right shift operation between two or more operands

See the following example of bitwise operator:

  
  #include<stdio.h>
  int main(){
   unsigned int a = 60;    // binary: 0011 1100
   unsigned int b = 13;    // binary: 0000 1101
   int result = 0;

   result = a & b;         // bitwise AND: 0000 1100
   printf("a & b = %d\n", result);

   result = a | b;         // bitwise OR: 0011 1101
   printf("a | b = %d\n", result);

   result = a ^ b;         // bitwise XOR: 0011 0001
   printf("a ^ b = %d\n", result);

   result = ~a;            // bitwise NOT: 1100 0011
   printf("~a = %d\n", result);

   result = a << 2;        // bitwise left shift: 1111 0000
   printf("a << 2 = %d\n", result);

   result = a >> 2;        // bitwise right shift: 0000 1111
   printf("a >> 2 = %d\n", result);

   return 0;
  }
  

Output:

  a & b = 12
  a | b = 61
  a ^ b = 49
  ~a = -61
  a << 2 = 240
  a >> 2 = 15                           

Unary Operators:

Unary operators are operators that operate on a single operand. In C, there are several unary operators that can be used to perform different operations on variables or expressions.

Operator Name Description
++ Increment Operator This operator is used to increase the value of a variable by 1. It can be used as a prefix (++x) or postfix (x++). For example, if x = 5, then ++x will make x = 6, and x++ will make x = 6 as well.
-- Decrement Operator This operator is used to decrease the value of a variable by 1. It can be used as a prefix (--x) or postfix (x--). For example, if x = 5, then --x will make x = 4, and x-- will make x = 4 as well.

See the following example of unary operator:

  
  #include<stdio.h>
  int main(){
   int num = 5;
    printf("num before prefix increment: %d\n", num);
    printf("num after prefix increment: %d\n", ++num);

    num = 5;

    // postfix increment operator
    printf("num before postfix increment: %d\n", num);
    printf("num after postfix increment: %d\n", num++);
    printf("num after second read: %d\n", num);
    return 0;
  }
  

Output:

 num before prefix increment: 5
 num after prefix increment: 6
 num before postfix increment: 5
 num after postfix increment: 5
 num after second read: 6                         

Ternary Operator:

A ternary operator is a conditional operator that takes three operands. It's also known as the conditional operator because it evaluates a condition and returns one value if the condition is true, and another value if the condition is false.

The ternary operator in C takes the form of:

condition ? value1 : value2

Here, the condition is evaluated first. If it's true, the expression returns value1; otherwise, it returns value2. One advantage of using the ternary operator is that it allows for more concise code, especially for simple conditional statements.

See the following example of ternary operator:

  
  #include<stdio.h>
  int main(){
  int x = 10, y = 5, max;
  max = (x > y) ? x : y;
  printf("Max = %d",max)
   return 0;
  }
  

Output:

 Max = 10