Increment Operator in C
Learn C - C tutorial - Increment operator in c - C examples - C programs
Increment Operator in C - Definition and Usage
- In C- Programming Increment operator (++) is used to increase the value of the variable by one.
- In C- Programming decrement operator (--) is used to decrease the value of the variable by one.
C Syntax
S. No | Operator | Operator Type | Description | `
---|---|---|---|
1 | Pre increment | ++i | Value of i is incremented before assigning it to variable i. |
2 | Post increment | ++i | Value of i is incremented after assigning it to variable i. |
3 | Pre decrement | --i | Value of i is decremented before assigning it to variable i |
4 | Post decrement | i-- | Value of i is decremented after assigning it to variable i. |
Sample - C Code
C Code - Explanation
- In this statement we declared and assigned the value “1” for the variable “a”.
- In this printf statement, the post increment operation will be performed where the “a” value is incremented and stored in the variable(a=2), since it is post increment so the value will be printed only for the current value of “a”(a=1).
- In this printf statement, the pre increment operation will be performed where the value of “a=2” because previously the value will be increment which will be stored in the variable “a”, so now the value printed in the console display window will be “3” (a=3becasuse of pre increment).
- In this printf statement, the pre decrement operation will be performed where the value of “a” is 3 (a=2 it has been pre decremented), so the value printed in the console display window for “a” will be “2”.
- In this, the printf statement post decrement operation will be performed where the value of “a” is 2, since it is the post decrement operation the value of a=1 is decremented and stored in the variable. In the output the current value of “a=2” will be printed
Sample Output - Programming Examples
- Here in this output the post increment operation will be performed so the variable “a” value (1) will be displayed.
- Here in this output the pre increment operation will be performed so the variable “a” value (3) will be displayed.
- Here in this output the pre decrement operation will be performed so the variable “a” value (2) will be displayed.
- Here in this output the post decrement operation will be performed so the variable “a” value (2) will be displayed.