Operators in C Programming

June 30, 2018 | 1 Tags | 0 Comment

Operators in C Programming
Arithmetic Operators

These are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.

Operator Description
+ addition operator
- subtraction operator
/ division operator
* multiplication operator
++ increment operator increases the integer value by one
-- decrement operator decreases the integer value by one
#include <stdio.h>

int main()
{
   int a=40, b=20, add, sub, mul, div, mod;
   add = a + b;
   sub = a - b;
   mul = a * b;
   div = a / b;
   mod = a % b;

   printf("%d + %d : %d\n", a, b, add);
   printf("%d - %d : %d\n", a, b, sub);
   printf("%d * %d : %d\n", a, b, mul);
   printf("%d / %d : %d\n", a, b, div);
   printf("%d mod %d : %d\n", a, b, mod);

   return 0;
}

The difference between pre/post increment or decrement operators

Operator Description
Pre increment operator (++i) value of i is incremented before assigning it to the variable i
Post increment operator (i++} value of i is incremented after assigning it to the variable i
Pre decrement operator (--i) value of i is decremented before assigning it to the variable i
Post decrement operator (i--) value of i is decremented after assigning it to the variable i

Example for pre-increment operator

#include <stdio.h>

int main()
{
   int i=0;
   while(++i < 5 )
   {
       printf("%d ",i);
   }

   return 0;
}

Output:

1 2 3 4

How the code works

  • Step 1: value of i is incremented from 0 to 1 using pre-increment operator
  • Step 2: this incremented value 1 is compared with 5 in while expression
  • Step 3: then, this incremented value 1 is assigned to the variable i
  • Above 3 steps is continued until while expression becomes false

Example for post-increment operator

#include <stdio.h>

int main()
{
   int i=0;
   while(i++ < 5 )
   {
       printf("%d ", i);
   }

   return 0;
}

Output:

1 2 3 4 5

How the code works

  • Step 1: value of i is compared with 5 in while expression
  • Step 2: then, the value of i is incremented from 0 to 1 using post-increment operator
  • Step 3: then, this incremented value 1 is assigned to the variable i
  • Above 3 steps is continued until while expression becomes false

Example for pre-decrement operator

#include <stdio.h>

int main()
{
   int i=10;
   while(--i > 5 )
   {
       printf("%d ",i);
   }
   return 0;
}

Output:

9 8 7 6

How the code works

  • Step 1: value of i is decremented from 10 to 9 using pre-decrement operator
  • Step 2: this decremented value 9 is compared with 5 in while expression
  • Step 3: then, this decremented value 9 is assigned to the variable i
  • Above 3 steps is continued until while expression becomes false

Example for post-decrement operator

#include <stdio.h>

int main()
{
   int i=10;
   while(i-- > 5 )
   {
       printf("%d ",i);
   }
   return 0;
}

Output:

9 8 7 6 5

How the code works

  • Step 1: value of i is compared with 5 in while expression
  • Step 2: then, the value of i is decremented from 10 to 9 using post-decrement operator
  • Step 3: then, this decremented value 9 is assigned to the variable i
  • Above 3 steps is continued until while expression becomes false
Bitwise Operators

These are used to perform bit operations on given two variables.

Operator Description
& binary AND operator
^ binary OR operator
| binary XOR operator
~ binary NOT operator
<< binary left shift operator
>> binary right shift operator
#include <stdio.h>

int main()
{
   unsigned int a = 60;	/* 60 = 0011 1100 */
   unsigned int b = 13;	/* 13 = 0000 1101 */
   int c = 0;

   c = a & b;       /* 12 = 0000 1100 */
   printf("%d & %d = %d\n", a, b, c);

   c = a | b;       /* 61 = 0011 1101 */
   printf("%d | %d = %d\n", a, b, c);

   c = a ^ b;       /* 49 = 0011 0001 */
   printf("%d ^ %d = %d\n", a, b, c);

   c = ~a;          /*-61 = 1100 0011 */
   printf("~%d = %d\n", a, c);

   c = a << 2;     /* 240 = 1111 0000 */
   printf("%d << 2 = %d\n", a, c);

   c = a >> 2;     /* 15 = 0000 1111 */
   printf("%d >> 2 = %d\n", a, c);

   return 0;
}
Assignment Operators

These are used to assign the values for the variables.

#include <stdio.h>

int main()
{
   int a = 4, b;
   b = a;
   printf("a = %d; b = a; b = %d\n", a, b);
   b += a;
   printf("a = %d; b += a; b = %d\n", a, b);   // b += a is same as b = b + a
   b -= a;
   printf("a = %d; b -= a; b = %d\n", a, b);   // b -= a is same as b = b - a
   b *= a;
   printf("a = %d; b *= a; b = %d\n", a, b);   // b *= a is same as b = b * a
   b /= a;
   printf("a = %d; b /= a; b = %d\n", a, b);    // b /= a is same as b = b / a
   b %= a;
   printf("a = %d; b %%= a; b = %d\n", a, b);   // b %= a is same as b = b % a
   b <<= a;
   printf("a = %d; b <<= a; b = %d\n", a, b);   // b <<= a is same as b = b << a
   b >>= a;
   printf("a = %d; b >>= a; b = %d\n", a, b);   // b >>= a is same as b = b >> a
   b &= a;
   printf("a = %d; b &= a; b = %d\n", a, b);   // b &= a is same as b = b & a
   b ^= a;
   printf("a = %d; b ^= a; b = %d\n", a, b);   // b ^= a is same as b = b ^ a
   b |= a;
   printf("a = %d; b |= a; b = %d\n", a, b);   // b |= a is same as b = b | a

   return 0;
}
Relational Operators

These are used to compare the value of two variables.

Operator Description
== check if the values of two variables equal or not
!= check of the values of two variables equal or not
> check if the value of left variable is greater than the value of right variable
< check if the value of left variable is less than the value of right variable
>= check if the value of left variable is greater than or equal to the value of right variable
<= check if the value of left variable is less than or equal to the value of right variable
#include <stdio.h>

int main()
{
   int a = 21;
   int b = 10;

   if( a == b ) {
     printf("%d is equal to %d\n", a, b);
   } else {
     printf("%d a is not equal to %d\n", a, b);
   }

   if ( a < b ) {
     printf("%d is less than %d\n", a, b);
   } else {
     printf("%d is not less than %d\n", a, b);
   }

   if ( a > b ) {
     printf("%d is greater than %d\n", a, b);
   } else {
     printf("%d is not greater than %d\n", a, b);
   }

   /* Lets change value of a and b */
   a = 5;
   b = 20;

   if ( a <= b ) {
     printf("%d is either less than or equal to %d\n", a, b);
   } else {
     printf("%d is neither less than nor equal to %d\n", a, b);
   }

   if ( a >= b ) {
     printf("%d is either greater than  or equal to %d\n", a, b);
   } else {
     printf("%d is neither greater than nor equal to %d\n", a, b);
   }

   return 0;
}
Logical Operators

These are used to perform logical operations on the given two variables.

Operator Description
&& logical AND operator
|| logical OR operator
! logical NOT operator
#include <stdio.h>

int main()
{
   int m = 40, n = 20;
   int o = 20, p = 30;
   if (m > n && m != 0)
   {
     printf("%d > %d && %d != 0\n", m, n, m);
   }
   if (o > p || p != 20)
   {
     printf("%d > %d || %d != 20\n", o, p, p);
   }
   if (!(m > n && m != 0))
   {
     printf("!(%d > %d && %d != 0)\n", m, n, m);
   }

   return 0;
}
Ternary Operator

The syntax of C ternary operator is as follows

condition ? expression 1 : expression 2

If the condition is evaluated to be true, expression 1 is evaluated. If the condition is evaluated is to be false, expression 2 is evaluated.

#include <stdio.h>
#include <stdlib>

/**
 * Find min of two integers
 * @param x
 * @param y
 * @return minimum value of x and y
 */
inline int min(int x, int y) { return x <= y ? x : y; }

int main(int argc, char** argv) {
    int x = 10;
    int y = 15;

    printf("x = %d, y = %d\n", x, y);
    printf("min(x, y) = %d\n", min(x, y));

    return (0);
}
C
Samuel Yang image
Samuel Yang

If you like this tutorial, you can support me

Donate Now