C has two special unary operators called increment (`++`

) and decrement (`--`

) operators. These operators increment and decrement value of a variable by `1`

.

`++x`

is same as `x = x + 1`

or `x += 1`

`--x`

is same as `x = x - 1`

or `x -= 1`

Increment and decrement operators can be used only with variables. They can’t be used with constants or expressions.

1 2 3 4 5 6 7 |
int x = 1, y = 1; ++x; // valid ++5; // invalid - increment operator operating on a constant value ++(x+y); // invalid - increment operating on an expression |

Increment/Decrement operators are of two types:

- Prefix increment/decrement operator.
- Postfix increment/decrement operator.

Let’s start with the first one.

## Prefix increment/decrement operator

The prefix increment/decrement operator immediately increases or decreases the current value of the variable. This value is then used in the expression. Let’s take an example:

1 |
y = ++x; |

Here first, the current value of `x`

is incremented by `1`

. The new value of `x`

is then assigned to `y`

.

Similarly, in the statement:

1 |
y = --x; |

the current value of `x`

is decremented by `1`

. The new value of `x`

is then assigned to `y`

.

The following program demonstrates prefix increment/decrement operator in action:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main() { int x = 12, y = 1; printf("Initial value of x = %d\n", x); // print the initial value of x printf("Initial value of y = %d\n\n", y); // print the initial value of y y = ++x; // increment the value of x by 1 then assign this new value to y printf("After incrementing by 1: x = %d\n", x); printf("y = %d\n\n", y); y = --x; // decrement the value of x by 1 then assign this new value to y printf("After decrementing by 1: x = %d\n", x); printf("y = %d\n\n", y); // Signal to operating system everything works fine return 0; } |

**Expected Output:**

1 2 3 4 5 6 7 8 |
Initial value of x = 12 Initial value of y = 1 After incrementing by 1: x = 13 y = 13 After decrementing by 1: x = 12 y = 12 |

## Postfix Increment/Decrement operator

The postfix increment/decrement operator causes the current value of the variable to be used in the expression, then the value is incremented or decremented. For example:

1 |
y = x++; |

Here first, the current value of `x`

is assigned to `y`

then `x`

is incremented.

Similarly, in the statement:

1 |
y = x--; |

the current value of `x`

is assigned to `y`

then `x`

is decremented.

The following program demonstrates postfix increment/decrement operator in action:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main() { int x = 12, y = 1; printf("Initial value of x = %d\n", x); // print the initial value of x printf("Initial value of y = %d\n\n", y); // print the initial value of y y = x++; // use the current value of x then increment it by 1 printf("After incrementing by 1: x = %d\n", x); printf("y = %d\n\n", y); y = x--; // use the current value of x then decrement it by 1 printf("After decrementing by 1: x = %d\n", x); printf("y = %d\n\n", y); // Signal to operating system everything works fine return 0; } |

**Expected Output:**

1 2 3 4 5 6 7 8 |
Initial value of x = 12 Initial value of y = 1 After incrementing by 1: x = 13 y = 12 After decrementing by 1: x = 12 y = 13 |

## Precedence

The increment and decrement operators have higher precedence than the operators we have discussed so far (with the only exception being the parentheses). Further, Postfix increment/decrement operators have higher precedence than the prefix increment/decrement operators.

The following table lists the precedence and associativity of operators we have discussed so far:

Operators | Description | Associativity |
---|---|---|

`()` |
parentheses | left to right |

`++` , `--` |
postfix increment operator, postfix decrement operator | left to right |

`++` , `--` , `+` , `-` |
prefix increment operator, prefix decrement operator, unary plus, unary minus | right to left |

`*` , `/` , `%` |
Multiplication, Division and Modulus | left to right |

`+` , `-` |
Addition and Subtraction | left to right |

`=` , `+=` , `-=` , `*=` , `/=` , `%=` |
Assignment Operator and Compound assignment operator | right to left |

Let’s take some expression and solve them on the basis of operator precedence.

**Example 1**:

1 2 3 4 |
int x, y, z; x = 5; y = 8; z = ++x + y++; |

Solution:

Step 1: Evaluate `y++`

. Since `++`

is postfix, the current value of `y`

will be used in the expression and then it will be incremented.

1 |
z = ++x + 8; |

Step 2: Evaluate `++x`

. Since `++`

is prefix, the value of `x`

will be incremented immediately.

1 |
z = 6 + 8; |

Step 3: Evaluate `6 + 8`

.

1 |
z = 14; |

**Example 2**:

1 2 3 4 5 |
int a, b, c; a = 10; b = 20; c = 1; c += a++ * 5 - --b; |

Solution:

Step 1: Evaluate `a++`

. Since `++`

is postfix, the current value of `a`

will be used in the expression then it will be incremented. The expression now becomes:

1 |
c += 10 * 5 - --b; |

Step 2: Evaluate `--b`

. Since `--`

is prefix, the value of `b`

will be decremented immediately. The expression now becomes:

1 |
c += 10 * 5 - 19; |

Step 3: Evaluate `10 * 5`

.

1 |
c += 50 - 19; |

Step 4: Evaluate `50 - 19`

.

1 |
c += 31; |

Step 5: Evaluate `+=`

.

1 |
c = 32; |

Z=2

y=3

Z=Z++ + y++

what is answer to it?

5

z=6, y=4

3

given that

z=2; z=z++; z=3;

y=3; y=y++; y=4;

z=z++ +y+++

find the value of given that

z=z++ + y++ (z=2, y=3)

=2+3

=5

#include<stdio.h>

int main()

{

int a=5;

printf(a++,++a, a–, –a);

}

Output and explanation?

Hi neethu u have used uninary operator here.

you can try below as an exmple.

#include<stdio.h>

int main()

{

{

int a=5;

[/crayon]

}

output is = a++=5

output is = ++a=7

” =a– =7

” =–a =5

How this program works?

#include <stdio.h>

int main()

{

int i=10;

printf(“%d %d %d %d %d”,i++,++i,i,++i,i++);

}

Output 13 14 14 14 10

actually the output will be compiler dependent but remember i++ prints i value and then only increements but ++i increements and prints i value

Tell me,

int a = 1;

int b =1;

int c = a++ || b++;

in case of c, which one will get evaluate first whether b++ or a++

a++ will get evaluated and as it is one and || logical operator is of left to right associativity so if first operand is one it omits the second operand which is discussed in lazy operator evaluation