Ads

Increment and Decrement Operators



INCREMENT AND DECREMENT OPERATORS:

- Increment and decrement operators are denoted by two plus symbols or minus symbols respectively.

- Increment and decrement operators are unary operators, i.e. they operate on single operand only.

- Increment and decrement operators change the value in memory unlike other operators, which doesn’t alter the value in memory.

- They are divided into two types i) POST and  ii) PRE


PRE INCREMENT AND PRE DECREMENT: 

- Pre-increment and decrement operators operate a specific operation where the value of the variable is incremented first and then used in further calculation.

- In pre increment or decrement operators the operator ++ or –- are indicated before the operand. Like ++x, --x.

Syntax:

<operator> <operand>;

- Angle brackets are used for demonstration purpose only and should not be used in your c program.

-  For understanding let us take an example

x=5;
y=++x;

-  In above statements first we are assigning a value 5 to x and then in second statement we are incrementing x value from 5 to 6 and then assigning it to y. So after the above two statements the value in x is 6 and the value in y is also 6.

Program:


/* Program to check the operation of pre increment operator*/

#include<stdio.h>
int main()
{
 int x=5,y; /*initializing x and declaring y*/
 printf("The value in x is %d\n",x); /*Printing value of x before increment */
 y=++x; /* Pre incrementing the x value and same time assigning it to y*/
 printf("After incrementing the value in x is %d and in y is %d\n",x,y); /*Printing value of x and y after increment */

 return 0;
}

Output:

The value in x is 5
After incrementing the value in x is 6 and in y is 6

- Let us consider another similar example

x=5;
y=--x;

- In above statements first we are assigning a value 5 to x and then in second statement we are decrementing x value from 5 to 4 and then assigning it to y. So after the above two statements the value in x is 4 and the value in y is also 4.

Program:


/* Program to check the operation of pre decrement operator*/

#include<stdio.h>
int main()
{
 int x=5,y; /*initializing x and declaring y*/
 printf("The value in x is %d\n",x); /*Printing value of x before decrement */
 y=--x; /* Pre decrementing the x value and same time assigning it to y*/
 printf("After decrementing the value in x is %d and in y is %d\n",x,y); /*Printing value of x and y after decrement */

 return 0;
}

Ouput:

The value in x is 5
After decrementing the value in x is 4 and in y is 4


POST INCREMENT AND PRE DECREMENT:

- Post-increment and decrement operators operate a specific operation where the value of the variable is assigned first and then incremented.

- In post increment or decrement operators the operator ++ or – are indicated after the operand. Like x++, x--.

Syntax:

<operand><operator>; 

- Angle brackets are used for demonstration purpose only and should not be used in your c program

- For understanding let us take an example

x=5;
y=x++;

- In above statements first we are assigning a value 5 to x and then in second statement we are assigning the x value to y and then incrementing it to 6. So after the above two statements the value in x is 6 and the value in y is 5.

Program:


/* Program to check the operation of post increment operator*/

#include<stdio.h>
int main()
{
 int x=5,y; /*initializing x and declaring y*/
 printf("The value in x is %d\n",x); /*Printing value of x before increment */
 y=x++; /* Post incrementing the x value and same time assigning it to y*/
 printf("After incrementing the value in x is %d and in y is %d\n",x,y); /*Printing value of x and y after increment */

 return 0;
}


Output:
The value in x is 5
After incrementing the value in x is 6 and in y is 5

- Another simillar example

x=5;
y=x--;

- In above statements first we are assigning a value 5 to x and then in second statement we are assigning  x value to y and then decrementing it to 4. So after the above two statements the value in x is 4 and the value in y is 5.

Program:


/* Post decrement */
#include<stdio.h>
int main()
{
 int x=5,y; /*initializing x and declaring y*/
 printf("The value in x is %d\n",x); /*Printing value of x before decrement */
 y=x--; /* Post decrementing the x value and same time assigning it to y*/
 printf("After decrementing the value in x is %d and in y is %d\n",x,y); /*Printing value of x and y after decrement */

 return 0;
}


Output:
  The value in x is 5
  After decrementing the value in x is 4 and in y is 5



No comments:

Post a Comment