Increment and Decrement Operators in C

The increment and decrement operators provide a succinct way of incrementing and decrementing the value of a variable.The symbol for the increment operator is ++, and the symbol for the decrement operator is –. They are both unary operators.

#include <stdio.h>

int main(void){
  
  int i;
  
  i = 404;
  
  printf("Value: \t %d\n", i);
  
  /*
   * increment value of i
   */
  i++;
  
  printf("Increment Once: \t %d\n", i);
  
  /*
   * increment value of i again.
   */
  i++;
  
  printf("Increment Twice: \t %d\n", i);
  
  
  return 0;
   
}

The increment and decrement operator can be placed in either the prefix or the postfix position. The prefix position places the ++ or — in front of the variable, and the postfix position places the ++ or — after the variable.

#include <stdio.h>

void printValue(int a);

int main(void){
  
  int a = 5;
  
  printValue(a);
  
  a--;
  
  printValue(a);
  
  /*
   * decrement operator
   * in the postfix position
   */
  --a;
  
  printValue(a);
  
  printValue(--a);
  
  return 0;
  
}


void printValue(int a){
  
 printf("Value = %d\n", a); 
  
}

 

In many cases, we don’t care where we put the increment or decrement operator, we just care that it is placed there. After all, the main point of an increment or decrement operation is to increase or decrease the value stored in the variable.  This change in the variable’s stored value is not the result of the operation, but rather it’s aftermath, and here’s where using the ++ and — operators gets a little tricky.

#include <stdio.h>

int main(void){
  
  int i = 1701;
  int j = i;
  int k = 0;
  
  
  /*
   * prefix
   */
  --k;
  /*
   * postfix
   */
  k--;
  
  printf("k = %d\n", k);
  printf("k = %d\n", --k);
  
  k = ++i;
  printf("i = %d \t k = %d\n", i, k);
  
  k = j++;
  /*
   * this line will print
   * the same value as the one
   * above it
   */
  printf("i = %d \t j = %d\n", i, j);
  

  return 0;
  
}

When the result of the ++ and — operation is important, we must be clear on where we place the increment and decrement operators and why. If we place them before the variable, than the result is one plus or minus the value stored in the variable; if we place them after the variable, the result is just the value stored in the variable. Note that in both cases the value stored in the variable has been changed by the same amount, after the expression has been resolved. We can see this effect quite clearly when the ++ or — operators are used in function calls or assignment operations.

#include <stdio.h>

int main(void){
  
  int x, y;
  
  x = y = 73;
  
  printf("\n Pre \t Post\n");
  printf(" %d \t %d \n", x++, ++y);
  printf(" %d \t %d \n", x++, ++y);
  
  printf("\n %d \t %d\n", x--, --y);
  printf(" %d \t %d\n", x--, --y);
  
  
  return 0;
  
}

Again, the increment and decrement operators differ in terms of when the operation is performed. When placed before the variable, the increment and decrement operators modify the value stored in the variable before it is used in any enclosing expressions in the statement. When placed after the variable, the increment and decrement operators modify the value stored in the variable after it is used in any enclosing expressions in the statement.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s