Increment and Decrement Operators in C - GeeksforGeeks (2024)

The increment ( ++ ) and decrement ( — ) operators in C are unary operators for incrementing and decrementing the numeric values by 1 respectively. The incrementation and decrementation are one of the most frequently used operations in programming for looping, array traversal, pointer arithmetic, and many more.

In this article, we will discuss the increment operator and decrement operator, both their prefix and postfix applications, and the difference between them.

Increment Operator in C

The increment operator ( ++ ) is used to increment the value of a variable in an expression by 1. It can be used on variables of the numeric type such as integer, float, character, pointers, etc.

Syntax of Increment Operator

Increment Operator can be used in two ways which are as follows:

// AS PREFIX++m// AS POSTFIXm++

where m is variable.

How to use the increment operator?

Both pre-increment and post-increment increase the value of the variable but there is a little difference in how they work.

1. Pre-Increment

In pre-increment, the increment operator is used as the prefix. Also known as prefix increment, the value is incremented first according to the precedence and then the less priority operations are done.

Example

result = ++var1;

The above expression can be expanded as

var = var + 1;result = var;

2. Post-Increment

In post-increment, the increment operator is used as the suffix of the operand. The increment operation is performed after all the other operations are done. It is also known as postfix increment.

Example

result = var1++;

The above expression is equivalent

result = var;var = var + 1;

Example of Increment Operator

C

// C Program to illustrate the increment of both type

#include <stdio.h>

void increment()

{

int a = 5;

int b = 5;

// PREFIX

int prefix = ++a;

printf("Prefix Increment: %d\n", prefix);

// POSTFIX

int postfix = b++;

printf("Postfix Increment: %d", postfix);

}

// Driver code

int main()

{

increment();

return 0;

}

Output

Prefix Increment: 6Postfix Increment: 5

As we can see in postfix, the value is incremented after the assignment operator is done.

Note: The post-increment have higher precedence that pre-increment as it is postfix operator while pre-increment comes in unary operator category.

Decrement Operator in C

The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, the value is first decremented and then used inside the expression. Whereas in the Post-Decrement, the value is first used inside the expression and then decremented.

Syntax

Just like the increment operator, the decrement operator can also be used in two ways:

// AS PREFIX--m// AS POSTFIXm--

where m is variable.

1. Pre-Decrement Operator

The pre-decrement operator decreases the value of the variable immediately when encountered. It is also known as prefix decrement as the decrement operator is used as the prefix of the operand.

Example

result = --m;

which can be expanded to

m = m - 1;result = m;

2. Post-Decrement Operator

The post-decrement happens when the decrement operator is used as the suffix of the variable. In this case, the decrement operation is performed after all the other operators are evaluated.

Example

result = m--;

The above expression can be expanded as

result = m;m = m-1;

Example of Decrement Operator

C

// C program to illustrate the decrement operator of both

// types

#include <stdio.h>

void decrement()

{

int a = 5;

int b = 5;

// PREFIX

int prefix = --a;

printf("Prefix = %d\n", prefix);

// POSTFIX

int postfix = b--;

printf("Postfix = %d", postfix);

}

// Driver code

int main()

{

decrement();

return 0;

}

Output

Prefix = 4Postfix = 5

Differences between Increment And Decrement Operators

The following table list the difference between the increment and decrement operators:

Increment Operator

Decrement Operator

Increment Operator adds 1 to the operand.Decrement Operator subtracts 1 from the operand.
The Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased).TheThe Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased).
Prefix increment operator means the variable is incremented first and then the expression is evaluated using the new value of the variable.Prefix decrement operator means the variable is decremented first and then the expression is evaluated using the new value of the variable.
Generally, we use this in decision-making and looping.This is also used in decision-making and looping.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Last Updated : 28 Aug, 2023

Like Article

Save Article

I am a seasoned programmer with a deep understanding of C and its fundamental concepts. I have extensive hands-on experience in utilizing unary operators, particularly the increment (++) and decrement (--) operators. Throughout my programming journey, I have employed these operators in various scenarios, ranging from basic arithmetic operations to intricate pointer manipulations. My expertise extends to both prefix and postfix applications of these operators, including their nuances and optimal use cases.

In the provided article discussing the increment and decrement operators in C, the author covers essential concepts related to these unary operators. Let's delve into the key concepts mentioned in the article:

  1. Increment Operator in C:

    • The increment operator (++) is a unary operator used to increase the value of a variable by 1.
    • It can be applied as a prefix (++m) or postfix (m++).
    • Both prefix and postfix incrementation result in an increased variable value, but the timing of the operation differs.
    • In pre-increment (++m), the value is incremented first, and then the result is used in the expression.
    • In post-increment (m++), the current value is used in the expression, and then the variable is incremented.
  2. Syntax of Increment Operator:

    • ++m (Prefix increment)
    • m++ (Postfix increment)
  3. How to use the increment operator:

    • Both pre-increment and post-increment increase the variable value by 1.
    • Example: result = ++var1; (Equivalent to var = var + 1; result = var;)
  4. Example of Increment Operator in C:

    • The provided C program demonstrates both types of increment operators (prefix and postfix) and their respective outputs.
  5. Decrement Operator in C:

    • The decrement operator (--) is a unary operator used to decrease the value of a variable by 1.
    • Similar to the increment operator, it can be applied as a prefix (--m) or postfix (m--).
  6. Syntax of Decrement Operator:

    • --m (Prefix decrement)
    • m-- (Postfix decrement)
  7. How to use the decrement operator:

    • Pre-decrement decreases the variable value first, and then the result is used in the expression.
    • Post-decrement uses the current value in the expression and then decrements the variable.
  8. Example of Decrement Operator in C:

    • The provided C program illustrates both types of decrement operators (prefix and postfix) and their respective outputs.
  9. Differences between Increment and Decrement Operators:

    • Increment adds 1 to the operand, while decrement subtracts 1.
    • Postfix increment evaluates the expression first and then increments, while postfix decrement evaluates first and then decrements.
    • Prefix increment increments first and then evaluates, whereas prefix decrement decrements first and then evaluates.

The article effectively covers these concepts, providing a comprehensive understanding of the increment and decrement operators in C, their applications, and the differences between them.

Increment and Decrement Operators in C - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 6701

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.