Article by: Manish Methani
Last Updated: November 6, 2021 at 2:04pm IST
Increment and decrement operators are used to modify the value of a variable in C programming. They are unary operators, which means they operate on a single operand. The increment operator is denoted by "++" and the decrement operator is denoted by "--". In this tutorial, we will explore how to use increment and decrement operators in C programming with code examples.
int x = 5; int y = ++x; // x is now 6, y is also 6
int x = 5; int y = x++; // x is now 6, y is 5
2. Decrement Operator: The decrement operator decreases the value of a variable by 1. It can also be used in two ways: prefix and postfix.
int x = 5; int y = --x; // x is now 4, y is also 4
int x = 5; int y = x--; // x is now 4, y is 5
3. Using Increment and Decrement Operators in Loops: Increment and decrement operators are commonly used in loops to iterate over a sequence of values. Here is an example of using the postfix increment operator in a for loop:
for(int i = 0; i < 10; i++) { printf("%d ", i); } // Output: 0 1 2 3 4 5 6 7 8 9
Similarly, we can use the postfix decrement operator in a loop to iterate over a sequence of decreasing values.
for(int i = 10; i > 0; i--) { printf("%d ", i); } // Output: 10 9 8 7 6 5 4 3 2 1
In conclusion, increment and decrement operators are useful tools for modifying the value of a variable in C programming. They can be used in prefix or postfix notations and are commonly used in loops to iterate over a sequence of values. Remember to use them with care, as they can easily cause confusion and bugs in your code. With these concepts in your toolkit, you will be well on your way to becoming a proficient C programmer.
Level: Advanced
Course Fees: INR 6999/-
Apply Coupon Code: UNLOCK
4 days 100% Money Back Guarantee
Explore CurriculumLevel: Intermediate
Course Fees: INR 1299/-
4 days 100% Money Back Guarantee
Explore CurriculumLevel: Beginners
Course Fees: INR 1299/-
4 days 100% Money Back Guarantee
Explore CurriculumLevel: Beginners
Course Fees: INR 1299/-
4 days 100% Money Back Guarantee
Coming SoonLevel: Beginners
Course Fees: FREE
Start WatchingTest your C Programming skills with this comprehensive mock test on C Programming.
Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.
Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.
Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.
Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.
Take Test