The post increment operator is used to increment the value of some variable after using it in an expression. In the post increment the value is used inside the expression, then incremented by one. If the expression is a = b; and b is holding 5 at first, then a will also hold 5. Assignment Operator: The simple assignment operator (=) assigns the right side to left side. C provides shorthand operators that have the capability of performing an operation and an assignment at the same time. For example: int x=5; x+=3; //It's mean x=x+3 x-=2; //It's mean x=x-2.
Increment and Decrement Operator in C
Increment Operators are used to increased the value of the variable by one and Decrement Operators are used to decrease the value of the variable by one in C programs.
Both increment and decrement operator are used on a single operand or variable, so it is called as a unary operator. Unary operators are having higher priority than the other operators it means unary operators are executed before other operators.
Note: Increment and decrement operators are can not apply on constant.
Example
- The operator of increment is represented by two plus signs in a row. Examples: counter = counter + 1; counter += 1; counter; counter. As C statements, the four examples all do the same thing. They add 1 to the value of whatever is stored in counter. The decrement operator is represented by two minus signs in a row.
- Within the C programming language the increment and decrement are often used in this simple generic way. The operator of increment is represented by two plus signs in a row.
Type of Increment Operator
- pre-increment
- post-increment
pre-increment (++ variable)
In pre-increment first increment the value of variable and then used inside the expression (initialize into another variable).
Example pre-increment
In above program first increase the value of i and then used value of i into expression.
post-increment (variable ++)
In post-increment first value of variable is used in the expression (initialize into another variable) and then increment the value of variable.
Syntax
Output
In above program first used the value of i into expression then increase value of i by 1.
Type of Decrement Operator
- pre-decrement
- post-decrement
Pre-decrement (-- variable)
In pre-decrement first decrement the value of variable and then used inside the expression (initialize into another variable).
Example pre-decrement
In above program first decrease the value of i and then value of i used in expression.
post-decrement (variable --)
In Post-decrement first value of variable is used in the expression (initialize into another variable) and then decrement the value of variable.
Syntax
Output
In above program first used the value of x in expression then decrease value of i by 1.
Example of increment and decrement operator
Output
Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN
Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet
- C++ Basics
- C++ Object Oriented
Pre Increment In C
- C++ Advanced
- C++ Useful Resources
- Selected Reading
Increment And Decrement Operators
The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand. Thus −
How To Increment In C
And similarly −
Both the increment and decrement operators can either precede (prefix) or follow (postfix) the operand. For example −
or as −
When an increment or decrement is used as part of an expression, there is an important difference in prefix and postfix forms. If you are using prefix form then increment or decrement will be done before rest of the expression, and if you are using postfix form, then increment or decrement will be done after the complete expression is evaluated.
Example
Following is the example to understand this difference −
When the above code is compiled and executed, it produces the following result −