w3resource

C Increment and Decrement Operators with examples

Increment and Decrement Operators in C

Overview

C provides two unique unary operators: ++ (increment) and -- (decrement). These operators are used to add or subtract 1 to/from a variable, and they come in two forms: prefix and postfix.

  • ++m; or m++; — increments the value of m by 1.
  • --m; or m--; — decrements the value of m by 1.

Syntax and Explanation

  • Prefix: ++m or --m – The value is modified before it is used in an expression.
  • Postfix: m++ or m-- – The value is modified after it is used in an expression.

This tutorial demonstrates how to use the increment and decrement operators, both in simple statements and in expressions, along with explanations of how they work in different contexts.

Key Topics:

Prefix Increment Operator (++m)

This operator increments the value of the variable before it is used in an expression.

  • When to use: Use this when you need the variable's value to be incremented before using it in an expression or operation.
  • Why to use: It ensures that the updated (incremented) value is used in the current expression immediately.

Example: Prefix Increment Operator (++m)

This code demonstrates the prefix increment operator in C. The variable 'a' is initialized to 5. Using the prefix increment (++a), the value of 'a' is incremented by 1 before it is used in the printf() statement. As a result, 'a' becomes 6, and the program prints "Prefix increment: 6" to the console.

Code:

#include <stdio.h>
int main() {
    int a = 5;
    // Prefix increment: 'a' is incremented first, then used
    printf("Prefix increment: %d\n", ++a); // Output will be 6
    return 0;
}

Output:

Prefix increment: 6

Postfix Increment Operator (m++)

This operator increments the value of the variable after it is used in an expression.

  • When to use: Use this when you want to use the current value of the variable in the expression first, and increment the variable afterward.
  • Why to use: It is useful when you need the variable’s current value in an expression but want the variable to increase after the expression is evaluated.

Example 2: Postfix Increment Operator (m++)

Following code demonstrates the postfix increment operator in C. The variable a is initialized to 5. Using the postfix increment (a++), the value of 'a' is first printed as 5, then it is incremented by 1. In the next printf() statement, the new value of 'a' (which is now 6) is printed.

Code:

#include <stdio.h>
int main() {
    int a = 5;
    // Postfix increment: 'a' is used first, then incremented
    printf("Postfix increment: %d\n", a++); // Output will be 5
    printf("After increment: %d\n", a);     // Output will be 6
    return 0;
}

Output:

Postfix increment: 5
After increment: 6

Prefix Decrement Operator (--m)

This operator decrements the value of the variable before it is used in an expression.

  • When to Use: Use this when you need the variable's value to be decremented before using it in an expression or operation.
  • Why to Use: It ensures that the updated (decremented) value is used in the current expression immediately.

Example 3: Prefix Decrement Operator (--m)

This code demonstrates the prefix decrement operator in C. The variable a is initialized to 5. Using the prefix decrement (--a), the value of a is first decremented by 1, and then the new value (which is 4) is printed.

Code:

#include <stdio.h>
int main() {
    int a = 5;
    // Prefix decrement: 'a' is decremented first, then used
    printf("Prefix decrement: %d\n", --a); // Output will be 4
    return 0;
}

Output:

Prefix decrement: 4

Postfix Decrement Operator (m--)

This operator decrements the value of the variable after it is used in an expression.

  • When to Use: Use this when you want to use the current value of the variable in the expression first, and decrement the variable afterward.
  • Why to Use: It is useful when you need the variable’s current value in an expression but want the variable to decrease after the expression is evaluated.

Example 4: Postfix Decrement Operator (m--)

Code:

#include <stdio.h>
int main() {
    int a = 5;
    // Postfix decrement: 'a' is used first, then decremented
    printf("Postfix decrement: %d\n", a--); // Output will be 5
    printf("After decrement: %d\n", a);     // Output will be 4
    return 0;
}

Output:

Postfix decrement: 5
After decrement: 4

Using Increment in Expressions

Using ++m inside an expression affects the final result, as the increment happens before the expression is evaluated.

Example: Using Increment in Expressions

Following code demonstrates the prefix increment operator used in an expression. The variable 'a' is initialized to 5. In the expression b = ++a + 3, 'a' is incremented to 6 before being used. The expression then adds 6 and 3, resulting in 9, which is assigned to 'b'.

Code:

#include <stdio.h>

int main() {
    int a = 5, b;
    // Prefix increment in an expression
    b = ++a + 3; // 'a' is incremented to 6, then 6 + 3 = 9 is assigned to 'b'
    printf("a: %d, b: %d\n", a, b); // Output will be a: 6, b: 9
    return 0;
}

Output:

a: 6, b: 9

Using Decrement in Expressions

Using m-- inside an expression means the original value is used before decrementing.

Example: Using Decrement in Expressions

Following code demonstrates the postfix decrement operator used in an expression. The variable 'a' is initialized to 5. In the expression b = a-- + 3, a is first used as 5, so 5 + 3 = 8 is assigned to 'b'. After the expression is evaluated, 'a' is decremented to 4.

Code:

#include <stdio.h>
int main() {
    int a = 5, b;
    // Postfix decrement in an expression
    b = a-- + 3; // 'a' is used as 5 first, so 5 + 3 = 8 is assigned to 'b', then 'a' is decremented to 4
    printf("a: %d, b: %d\n", a, b); // Output will be a: 4, b: 8
    return 0;
}

Output:

a: 4, b: 8


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/c-increment-and-decrement-operators.php