Programming in C: Increment and Decrement

Last time we took a look at how to handle user input, today we will be looking at different ways to increment and decrement values in C. Incrementing or decrementing a value, like an integer, is increase or decreasing the value by one or more. Suppose you have the following program:

#include <stdio.h>
int main(void) 
{
  int number = 1;
printf("The number is: %d", number);
  
number++;
  printf(The number incremented by 1 is: %d", number);
number--;
  printf(The number decremented by 1 is: %d", number);
}

The way to increment and decrement a number above is the simplest way. There are many ways of incrementing or decrementing a number, as seen below:

Increment

variable_name = variable_name + 1;

variable_name += 1;

++variable_name;

Decrement 

variable_name = variable_name - 1;

variable_name -= 1;

--variable_name;

The difference between having the ++ or -- before or after the variable name are as follows:

ariable_name++; - Uses the variable then increments.
++variable_name; - Increments the value, then uses the variable.
variable_name--; - Uses the variable then decrements.
--variable_name; - Decrements the value, then uses the variable.

More tutorials coming soon! Let me know how you like the tutorials by commenting below and if there is any specific topics or languages you would like covered.

Follow me on Twitter and Google Plus to keep up-to-date on all the latest news and tutorials. If you have any suggestions feel free to let me know.

Programming in C: If Statements

Introduction to Bash Shell Scripting: Linux Commands - Part 2