Increment and Decrement in C
0 207
Increment (++) and decrement (--) operators in C are fundamental arithmetic operators used to modify the value of a variable.
The increment operator increases the value of a variable by 1, while the decrement operator decreases it by 1.
They can be used in various contexts like standalone statements, expressions, loops, and function arguments, and understanding their syntax and usage is essential for effective C programming.
Syntax:
1Increment Operator:
variable++; ++variable;
2Decrement Operator:
variable--; --variable;
Examples:
1Program for Increment Operator:
// Program for Increment Operator #include<stdio.h>int main() { int x = 5; // Post-increment int y = x++; printf("Post-increment: x = %d, y = %d\n", x, y); // Pre-increment int z = ++x; printf("Pre-increment: x = %d, z = %d\n", x, z); return 0; }
Output:
Post-increment: x = 6, y = 5 Pre-increment: x = 7, z = 7
2Program for Decrement Operator:
// Program for Decrement Operator #include<stdio.h>int main() { int x = 10; // Post-decrement int y = x--; printf("Post-decrement: x = %d, y = %d\n", x, y); // Pre-decrement int z = --x; printf("Pre-decrement: x = %d, z = %d\n", x, z); return 0; }
Output:
Post-decrement: x = 9, y = 10 Pre-decrement: x = 8, z = 8
Share:
Comments
Waiting for your comments