Increment and decrement operators have 2 variation or types
- prefix
- postfix
In prefix, operators are written before their operands.
Example:++10
In postfix, operators are written after their operands.
Example: 10++
Now we have already seen that 10++ and ++10 both will give us 11, then what’s the difference?
What will be the output?
C
1 2 3 | int x = 10; printf(“%d”,++x); |
The output will be
1 | 11 |
Again what will be the output?
C
1 2 3 | int x = 10; printf(“%d”,x++); |
Strangely the output will be
C
1 | 10 |
What happened actually?
In case 1, the value of x is incremented first and then the value 11 got printed, whereas in case 2, the value got printed first and the value incremented. So do you now understood the difference?
In prefix, the value of the variable will get incremented first and then the statement will get executed. Whereas in postfix, the statement will be executed with the current value of a variable and then the value gets incremented.
Let’s see these programs
Program: Postfix
1 2 3 4 5 6 7 8 9 10 11 | /********** Postfix **********/ #include<stdio.h> main() { . int x = 1; printf("%d\n" ,x++); printf("%d",x); getch(); } |
Output:
C
1 2 | 1 2 |
Program: Prefix
C
1 2 3 4 5 6 7 8 9 10 11 | /********** Prefix **********/ #include<stdio.h> main() { . int x = 1; printf("%d\n" ,++x); printf("%d",x); getch(); } |
Output:
C
1 2 | 2 2 |
So observing the output of the program, we can understand the difference between postfix and prefix.
Contributed by:Salim Sheikh
I'm an enthusiast with a solid background in C programming, and I've delved into the intricacies of increment and decrement operators. My expertise in this area allows me to provide a detailed analysis of the concepts discussed in the article you provided by Salim Sheikh.
In the article, Salim explores two variations of increment and decrement operators: prefix and postfix. These operators play a crucial role in C programming, influencing the order of execution and output in different scenarios.
Let's break down the key concepts mentioned in the article:
-
Prefix Increment Operator (++x):
- The prefix increment operator is written before the operand (e.g.,
++10
). - It increments the value of the variable first and then executes the statement.
- Example:
int x = 10; printf("%d", ++x); // Output: 11
- The prefix increment operator is written before the operand (e.g.,
-
Postfix Increment Operator (x++):
- The postfix increment operator is written after the operand (e.g.,
10++
). - It executes the statement with the current value of the variable and then increments the value.
- Example:
int x = 10; printf("%d", x++); // Output: 10
- The postfix increment operator is written after the operand (e.g.,
-
Difference Between Prefix and Postfix:
-
The article highlights a crucial difference in output based on the order of execution.
-
Example:
int x = 10; printf("%d", ++x); // Output: 11 printf("%d", x++); // Output: 10
-
In prefix, the value of the variable is incremented first, and then the statement is executed.
-
In postfix, the statement is executed with the current value of the variable, and then the value gets incremented.
-
-
Program Demonstrations:
-
Salim provides two program examples to illustrate the difference between postfix and prefix:
-
Postfix Program:
#include<stdio.h> int main(){ int x = 1; printf("%d\n", x++); printf("%d", x); getch(); }
Output:
12
-
Prefix Program:
#include<stdio.h> int main(){ int x = 1; printf("%d\n", ++x); printf("%d", x); getch(); }
Output:
22
-
-
By thoroughly understanding the nuances of prefix and postfix operators, developers can optimize their code and avoid unexpected outcomes. Salim Sheikh's article effectively conveys these concepts through practical examples. If you have any specific questions or if there's a particular aspect you'd like to delve into further, feel free to ask.