6.6 – Increment/ decrement Operators

by subbu on September 28, 2013

Increment/ decrement operators in C language:

  • ++, – – are unary operators used to increment, decrement the value of a variable by 1. Say for example if variable x is assigned with 10, evaluating x++ results increment of its value by 1 and evaluating x- – results decrement of its value by 1.
  • These operators can’t be used to increment or decrement the value of a constant or an expression.
  • These operators have right to left associativity.
  • These operators can be used as both prefix and postfix to the operands. If we use the operator to the left side of an operand then it is called prefix operator (++i, – -i). If we use the operator to the right side of an operand then it is called postfix operator (i++, i- -).

There is no difference among the postfix and prefix operators if there are no other operations are involved in an expression.

#include<stdio.h>
int main()
{
int a=10;
a++;                   /* increments by 1 */
printf("%d",a);
++a;                   /* increments by 1 */
printf("\n%d",a);
a--;                     /* decrements by 1 */
printf("\n%d",a);
--a;                     /* decrements by 1 */
printf("\n%d",a);
return 0;
}

Output:
11
12
11
10

Postfix vs Prefix:

When other operations like assigning, comparing or printing are involved in increment/decrement statements then there would be difference among the prefix and postfix operators.

Postfix operator increments or decrements the value of a variable by 1 after performing other operations like assigning, comparing or manipulating. Say for example y=x++; is a statement, value of x assigns to y before incrementing.

#include<stdio.h>
int main()
{
int x,y;
x=45;
y=x++;      /*  Increments after assignment */
printf("x=%d\ny=%d",x,y);
return 0;
}

Output:
x=46
y=45

#include<stdio.h>
int main()
{
int x=10;
printf("%d",x--);  /* Decrements after printing */
return 0;
}

Output:
10

Prefix operator Increments or decrements the value of a variable by 1 before performing other operations.

#include<stdio.h>
int main()
{
int x,y;
x=45;
y=++x;      /*  Increments before assignment */
printf("x=%d\ny=%d",x,y);
return 0;
}

Output:
46
46

#include<stdio.h>
int main()
{
int x=10;
if(--x<10)              /* decrements before checking condition */
  printf("Hello");
else
  printf("World");
return 0;
}

Output:
Hello

Quiz 1:
1) What would the output of following program?

#include<stdio.h>
int main()
{
int a=10,b,c;
b=a++;
c=++b;
printf("a=%d\nb=%d\nc=%d",a,b,c);
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=10,b=20;
printf("a=%d",++a);
printf("\nb=%d",b--);
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=10,b;
b=++a;
if(a<++b)
  printf("Hello");
else
  printf("World");
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=10;
if(10<++a)
  printf("a=%d",a);
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=0;
if(a++)
  printf("a of if %d",a);
else
  printf("a of else %d",a);
return 0;
}

6) What would be the output of following program?

#inclde<stdio.h>
int main()
{
int x=10,y=10;
x+++;
---y;
printf("x=%d\ny=%d",x,y);
return 0;
}
Show Answer
1)
Output:
a=11
b=11
c=11

b=a++;    //b=a    a=a+1
c=++b;    //b=b+1  c=b

2)
Output:
a=11
b=20

printf(“a=%d”,++a);  // a=a+1   printf(“%d”,a)
printf(“\nb=%d”,b–); // printf(“%d”,b)  b=b-1

3)
Output:
Hello
b=++a;       // a=a+1   b=a
if(a<++b)    // b=b+1   if(a<b)

4)
Output:
11

if(10<++a)               //a=a+1    if(10<11)
printf(“a=%d”,a);

5)
Output:
a of else 1
if(a++)     // if(a)    a=a+1

6) Error //no operators like +++, – – –

Previous post:

Next post: