C Increment and decrement operator

Description

Increment and decrement operators are

++

and

--

Syntax

The following code shows the postfix Increment and decrement operator.


var++;
var--;

The following code shows prefix Increment and decrement operator.


--var;
++var;

Difference

Different between Prefix and Postfix are.

  • Prefix: the value is incremented/decremented first and then applied.
  • Postfix: the value is applied and the value is incremented/decremented.

Example


#include<stdio.h>
/*from ww w.j a va 2  s . c  o  m*/
main( )
{
    int i = 3,j = 4,k;
    k = i++ + --j;
    printf("i = %d, j = %d, k = %d",i,j,k);
}

The code above generates the following result.

The decrement operator: --


#include <stdio.h>
#include <stdlib.h>
 /*  www. j  av  a2  s .c  o  m*/
int main()
{
    char weight[4];
    int w;
 
    printf("Enter your weight:");
    gets(weight);
    w=atoi(weight);
 
    printf("Here is what you weigh now: %i\n",w);
    w--;
    printf("w++: %i\n",w);
    w--;
    printf("w++: %i\n",w);

    return(0);
}

The code above generates the following result.





















Home »
  C Language »
    Language Basics »




C Language Basics
Data Types
Operator
Statement
Array