Understanding Pre/Post Increment And Decrement Operators In C With Examples

Pre/Post Increment And Decrements Operators

In looping we use counter variable to execute loop from beginning to end.To achieve the end we increments or  decrements the counter.In this chapter we will see different ways to increment or decrements variables. 

Pre Increment - 

In pre increment process increment takes place first and then assignment happens. Increment operator (++) comes before the counter variable.

Syntax : -

++counter_variable

#Program 1

Write a program to pre increment the value of i.

#include <stdio.h>


void main() {


int i = 5;

i = ++ i;

printf("%d",i);

}

This example shows that first i will be increased by 1 and then updated value will be assign to i. Output is 6. 

Post Increment -

In post increment process assignment takes place first and then increment happens. Increment operator (++) comes after the counter variable.

Syntax : -

counter_variable++

#Program 2

Write a program to post increment the value of i.

#include <stdio.h>


void main() {


int i = 5;


i =  i++;

printf("%d",i);

}

This example shows that first 5 will be assigned to i variable and then i will be incremented by 1. Output is 5.

Pre Decrement

In pre decrement process decrement takes place first and then 
assignment happensdecrement operator (--) comes before the counter variable.

Syntax : -

-- counter_variable

#Program 3

Write a program to pre decrement the value of i.

#include <stdio .h>


void main() {

int i = 5;


i = -- i;

printf("%d",i);

}

This example shows that first i will be decreased by 1 and then updated value will be assign to i. Output is 4. 

Post Decrement -

In post decrement process assignment takes place first and then 
decrement happens. Increment operator (--) comes after the counter variable.

Syntax : -

counter_variable --

#Program 4

Write a program to post decrement the value of i.

#include <stdio.h>


void main() {


int i = 5;


i = i--;


printf("%d",i);


}

This example shows that first 5 will be assigned to i variable and then i will be decremented by 1. Output is 5.

Some More Examples On Pre/Post Increment And Decrement

In interviews interviewer can ask some challenging questions based on pre/post
increment and decrement to check candidate's clarity on this topic.

#Program 5

#include <stdio.h>


void main() {


 int x = 2, y = 3, z;

 x = ++x;

 printf("x = %d ",x);


 y = --y;

 printf("y = %d\n",y);


 z = x++ + y--;

printf("z = %d\n",z);

printf("x = %d\n",x);

printf("y = %d\n",y);

}


#Explanation

Here x = 2 and y = 3

1. x = ++x -> pre-increment of x i.e. first increment then assignment

  x = 3

2. y = --y -> pre-decrement of y i.e. first decrement then assignment

  y= 2

3. z = x++ + y-- 

x++ -> post-increment of x i.e. first assignment then increment

after computation x will update.

y -- -> post-decrement of y i.e. first assignment then decrement 

after computation y will update.


z = 3 + 2 

z = 5

now x will be incremented by 1 and y will be decremented by 1

x = 4

y = 1


#Program 6

#include <stdio.h>


void main() {


int a=8;


for(int i=1;i<=3;i++){

if( ++a > 5 ){


  a++;

  }

}

printf("a = %d\n",a);

}


#Explanation


Here a = 8 


1. i = 1,

if ( ++a > 5) -> pre-increment of a (9 >5) condition true

a++ -> post increment (as per rule first assignment then increment) 

but here no assignment so that value will increase.

a = 10


2. i = 2,

if ( ++a > 5) -> pre-increment of a (11 > 5) condition true

a++ -> post increment (as per rule first assignment then increment) 

but here no assignment so that value will increase.

a = 12


3. i = 3,

if ( ++a > 5) -> pre-increment of a (13 > 5) condition true

a++ -> post increment (as per rule first assignment then increment) 

but here no assignment so that value will increase.

a = 14 (updated value of a)




Thanks for reading, please do share and comment your suggestions.

No comments

Powered by Blogger.