Postfix OperatorsPost-increment operator: A post-increment operator is used to increment the value of a variable after executing the expression in which the operator is used. With the post-increment operator, the value of the variable is first used in an expression and then incremented. Show
Syntax:
The value of Post-decrement operator: A post-decrement operator is used to decrement the value of a variable after executing the expression in which the operator is used. With the post-decrement operator, the value of the variable is first used in an expression and then decremented. Syntax:
The value of Prefix OperatorsPre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in a expression. With the pre-increment operator, the value of the variable is first incremented and then used in an expression. Syntax:
The value of Pre-decrement operator: A pre-decrement operator is used to decrement the value of a variable before using it in a expression. With the pre-decrement operator, the value of the variable is first decremented and then used in an expression. Syntax:
The value of Other DifferencesThe postfix operators have a higher precedence than the prefix operators, as well as a different associativity. The postfix operators are evaluated left-to-right while the prefix operators are evaluated right-to-left. Program Example
Output produced by the program: Value of x is 7 Value of x is now 7 Value of x is now 8 Value of x is now 9 Value of x is now 9 next → ← prev Operators are the predefined symbols of the C/C++ library, and it is used to perform logical as well as mathematical operations to the operands. There are various types of operators in the C programming language, such as arithmetic, logical, bitwise, increment or decrement operators, etc. Increment OperatorIncrement Operators are the unary operators used to increment or add 1 to the operand value. The Increment operand is denoted by the double plus symbol (++). It has two types, Pre Increment and Post Increment Operators. Pre-increment OperatorThe pre-increment operator is used to increase the original value of the operand by 1 before assigning it to the expression. Syntax In the above syntax, the value of operand 'A' is increased by 1, and then a new value is assigned to the variable 'B'. Example 1: Program to use the pre-increment operator in C Output Input the value of X: 10 Input the value of Y: 15 Input the value of Z: 20 The updated value of the X: 11 The updated value of the Y: 16 The updated value of the Z: 21 Post increment OperatorThe post-increment operator is used to increment the original value of the operand by 1 after assigning it to the expression. Syntax In the above syntax, the value of operand 'A' is assigned to the variable 'X'. After that, the value of variable 'A' is incremented by 1. Example 2: Program to use the post-increment operator in C Output Input the value of X: 10 Input the value of Y: 15 Input the value of Z: 20 The original value of a: 10 The original value of b: 15 The original value of c: 20 The updated value of the X: 11 The updated value of the Y: 16 The updated value of the Z: 21 Decrement OperatorDecrement Operator is the unary operator, which is used to decrease the original value of the operand by 1. The decrement operator is represented as the double minus symbol (--). It has two types, Pre Decrement and Post Decrement operators. Pre Decrement OperatorThe Pre Decrement Operator decreases the operand value by 1 before assigning it to the mathematical expression. In other words, the original value of the operand is first decreases, and then a new value is assigned to the other variable. Syntax In the above syntax, the value of operand 'A' is decreased by 1, and then a new value is assigned to the variable 'B'. Example 3: Program to demonstrate the pre decrement operator in C Output Input the value of X: 5 Input the value of Y: 6 Input the value of Z: 7 The updated value of the X: 6 The updated value of the Y: 7 The updated value of the Z: 8 Post decrement Operator:Post decrement operator is used to decrease the original value of the operand by 1 after assigning to the expression. Syntax In the above syntax, the value of operand 'A' is assigned to the variable 'B', and then the value of A is decreased by 1. Example 4: Program to use the post decrement operator in C Output Input the value of X: 6 Input the value of Y: 12 Input the value of Z: 18 The original value of a: 6 The original value of b: 12 The original value of c: 18 The updated value of the X: 5 The updated value of the Y: 11 The updated value of the Z: 17 Example 5: Program to perform the pre increment and pre decrement operator Output Enter the value of i 5 Enter the value of j 10 After using the pre-incrementing, the value of i is 6 The value of x is 6 After using the pre-decrementing, the value of j is 9 The value of y is 9 Example 6: Program to print the post increment and post decrement operator Output Enter the value of i 10 Enter the value of j 20 After using the post-incrementing, the value of i is 11 The value of x is 10 After using the post-decrementing, the value of j is 19 The value of y is 20 Difference between the Increment and Decrement Operator in C
Next TopicLogical AND Operator in C ← prev next → What is increment operator in C?Increment Operator is used to increase the value of the operand by 1 whereas the Decrement Operator is used to decrease the value of the operand by 1. In C++, the value of the variable is increased or decreased by 1 with the help of the Increment operator and the Decrement Operator.
What is the operator used for increment?The decrement (–) and increment (++) operators are special types of operators used in programming languages to decrement and increment the value of the given variable by 1 (one), respectively.
Does C have increment operators?The increment(++) and decrement operators(--) are important unary operators in C. Unary operators are those which are applied on a single operand. The increment operator increases the value of the variable by one and the decrement operator decreases the value of the variable by one.
|