Increment Operator in C



 increment-operator-in-c

Learn C - C tutorial - Increment operator in c - C examples - C programs

Increment Operator in C - Definition and Usage

  • In C- Programming Increment operator (++) is used to increase the value of the variable by one.
  • In C- Programming decrement operator (--) is used to decrease the value of the variable by one.
C Increment/Decrement Operator

C Syntax

Increment operator: ++var_name; (or) var_name++;
Decrement operator: –-var_name; (or) var_name–-; 
`
S. No Operator Operator Type Description
1 Pre increment ++i Value of i is incremented before assigning it to variable i.
2 Post increment ++i Value of i is incremented after assigning it to variable i.
3 Pre decrement --i Value of i is decremented before assigning it to variable i
4 Post decrement i-- Value of i is decremented after assigning it to variable i.

Sample - C Code

#include <stdio.h>
#include <conio.h>
void main()
{                    
    int a=1;
    printf ("Example of post increment-%d \n",a++);
    printf ("Example of pre increment-%d \n",++a);
    printf ("Example of pre decrement-%d \n",--a);
    printf ("Example of post decrement-%d \n",a--);
    getch();
}

C Code - Explanation

code-explanation-increment-decrement
  1. In this statement we declared and assigned the value “1” for the variable “a”.
  2. In this printf statement, the post increment operation will be performed where the “a” value is incremented and stored in the variable(a=2), since it is post increment so the value will be printed only for the current value of “a”(a=1).
  3. In this printf statement, the pre increment operation will be performed where the value of “a=2” because previously the value will be increment which will be stored in the variable “a”, so now the value printed in the console display window will be “3” (a=3becasuse of pre increment).
  4. In this printf statement, the pre decrement operation will be performed where the value of “a” is 3 (a=2 it has been pre decremented), so the value printed in the console display window for “a” will be “2”.
  5. In this, the printf statement post decrement operation will be performed where the value of “a” is 2, since it is the post decrement operation the value of a=1 is decremented and stored in the variable. In the output the current value of “a=2” will be printed

Sample Output - Programming Examples

code-explanation-increment-decrement-output
  1. Here in this output the post increment operation will be performed so the variable “a” value (1) will be displayed.
  2. Here in this output the pre increment operation will be performed so the variable “a” value (3) will be displayed.
  3. Here in this output the pre decrement operation will be performed so the variable “a” value (2) will be displayed.
  4. Here in this output the post decrement operation will be performed so the variable “a” value (2) will be displayed.

View More Quick Examples


Related Searches to c increment-decrement-operator