Increment operator in C
In C programming, increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1, respectively. These operators provide a shorthand way to perform operations like x = x + 1 or x = x - 1.
Both the output is same then what is the difference ?
Note:
- The output of pre-increment and post-increment is exactly same in the next line [new line] but whenever we are working in the same line in the execution then they both act differently.
1.Pre-Increment Operator:
The pre-increment operator (++x
) increases the value of a variable by 1 before using it in an expression. This means that the increment happens first, and then the updated value is used in further operations.
How pre-increment works ?
- The variable is incremented first.
- the updated value of variable is used in that expression. It is just like a mobile prepaid recharge, we first have to pay and then we can use the services
2.Post-Increment Operator:
The Post-increment operator (x++
) increases the value of te variable by 1, but after using the current value in the expression.That means the original value is used first, and then variable get incremented.
Practice Questions
- Given that int x=5,y; and the expression is y=x++*x++ ; what is the value of y?
- Given that int x=5,y; and the expression is y= ++x*x++ lwhat is the value of y?
- Given that int x=5,y; and the expression is y= x++*++x; what is the value of y?
- Given that int x=5,y; and the expression is y= ++x*++x; what is the value of y?
- Given that int x=1,y; and the expression is y= x++*++x*x++*++x*x++*x++; what is the value of y?
- Given that int x; x=5++ then what is the value of x;
- Given that int x=5,z=6,y; and the expression is y= ++z+x++ ;what is the value of y?
- Given that int x=5; ++++x then what is the value of x;
- Given that int x=5,y; and the expression y=!x++; what is the value of y?
- Given that int x=5,y; and the expression is ++x++; what is the value of x?
- Given that int x=5,y; and the expression is y= x+ ++x; what is the value of y?
- Given that int x=5,y; and the expression is y= x+++++x; what is the value of y?
- Given that int x=5,y; and the expression is y= x++ + ++x;what is the value of y?
What's Your Reaction?