blogger Indonesia

Follow judhyns blog

INCREMENT OPERATOR

Introduction

You can increment or decrement the value of variable using the increment or decrement operator. These operators can be applied only to variables and they can be applied using prefix form or postfix form.

Program

#include  main( ) { int I,j,k; i = 3; j =4; k = i++ + --j; printf("i = %d, j = %d, k = %d",i,j,k); } 
Input
i =3, j = 4. 
Output
i = 4, j = 3, k = 6. 

Explanation

When the prefix form is used, the value of the variable is incremented/decremented first and then applied. In the postfix form, the value is applied and only after the assignment operator is done is the value incremented or decremented.

  1. Suppose you write

    i = 3; j =4; k = i++ + -j; 

    you will get the value of k as 6, i as 4 and j as 3. The order of evaluation is as follows:

    1. i gets the value 3.

    2. j is decremented to 3.

    3. k gets the value 3 + 3.

    4. i is incremented.

  2. Suppose you write

    i = 5; i = i++ * i++ 

Then you will get the value of i as 27. This is because first the value 5 is used as to make i = 25 and then i is incremented twice. The increment and decrement operators have higher priority than the arithmetic operators.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |