0
0
Cprogramming~5 mins

Increment and decrement operators

Choose your learning style9 modes available
Introduction

Increment and decrement operators help you add or subtract 1 from a number quickly and simply.

Counting items in a list one by one.
Moving through positions in an array step by step.
Increasing or decreasing a score or value by 1.
Looping a fixed number of times.
Adjusting indexes when processing data.
Syntax
C
variable++;
variable--;
++variable;
--variable;

variable++ adds 1 after using the variable's current value.

++variable adds 1 before using the variable's value.

Examples
This adds 1 to x after its current value is used.
C
int x = 5;
x++;  // x becomes 6
This adds 1 to y before its value is used.
C
int y = 5;
++y;  // y becomes 6
This subtracts 1 from z after its current value is used.
C
int z = 5;
z--;  // z becomes 4
This subtracts 1 from w before its value is used.
C
int w = 5;
--w;  // w becomes 4
Sample Program

This program shows how the count variable changes when incremented and decremented using both postfix and prefix operators.

C
#include <stdio.h>

int main() {
    int count = 10;
    printf("Initial count: %d\n", count);

    count++;
    printf("After count++: %d\n", count);

    ++count;
    printf("After ++count: %d\n", count);

    count--;
    printf("After count--: %d\n", count);

    --count;
    printf("After --count: %d\n", count);

    return 0;
}
OutputSuccess
Important Notes

Postfix (variable++) uses the value first, then changes it.

Prefix (++variable) changes the value first, then uses it.

Use these operators to write shorter and clearer code when adding or subtracting 1.

Summary

Increment (++) adds 1; decrement (--) subtracts 1.

Postfix form changes value after use; prefix form changes before use.

They are useful for counting and looping tasks.