Signup/Sign In

Difference Between Increment and Decrement Operators in C

In C, the increment(++) and decrement(—) operators are essential unary operators. We utilize an increment operator to increase the value of any variable inside an expression by the specified amount. In contrast, a decrement operator decreases the total value of any variable included in an expression.

Following a quick introduction to increment and decrement operators, we will examine the whole list and the distinction between increment and decrement operators. Let's examine the differences between increment and decrement operators in detail.

What is Increment Operator?

In an expression, the increment operator is used to increase the value of a variable. The value is first increased and then utilised inside the expression in the Pre-Increment. In contrast, Post-Increment first uses the value inside the phrase before incrementing it.

Properties:

The principal characteristics of the preincrement and postincrement in C are as follows:

  • It adds 1 to the value of a variable.
  • It is only applicable to variables.
  • It is symbolized by two plus signs (++).

Example:

#include <stdio.h>

int increment(int a, int b)
{
	a = 5;

	// POSTFIX
	b = a++;
	printf("%d", b);

	// PREFIX
	int c = ++b;
	printf("\n%d", c);
}

// Driver code
int main()
{
	int x, y;
	increment(x, y);

	return 0;
}

Output:

5
6

What is Decrement Operator?

In an expression, the decrement operator is used to decrease the value of a variable. In the Pre-Decrement, the value is decremented before being included in the phrase. In contrast, Post-Decrement first uses the value inside the phrase before decrementing it.

Properties:

  • Decrement Operator is the unary operator used to decrease the value of the operand by one.
  • The symbol for the decrement operator is a double negative (—).
  • Pre Decrement and Post Decrement operators are its two categories.

Example:

#include <stdio.h>

int decrement(int a, int b)
{
	a = 5;

	// POSTFIX
	b = a--;
	printf("%d", b);

	// PREFIX
	int c = --b;
	printf("\n%d", c);
}

// Driver code
int main()
{
	int x, y;
	decrement(x, y);

	return 0;
}

Output:

5
4

Increment Operator vs. Decrement Operator

increment operator vs decrement operator

Increment Operator Decrement Operator
It is used to increase a variable's value by 1. It is used to lower the values of the operands by one.
The symbol for the increment operator is a double plus (++). The symbol for the decrement operator is a double negative (—).
It comes in two varieties: pre-increment and post-increment operators. Identical to the increment operator, it has two types: pre-decrement and post-decrement.
The pre increment operator indicates that the operator's value is incremented before to its usage in an expression.
The post-increment operator signifies that the operand is first utilised in the expression and then the original value is incremented by 1.
Pre decrement indicates that the operator's value is decremented before being assigned in the expression.
In contrast, the post decrement operator first uses the operand in the expression and then decrements the original value of the operand by 1.
X = ++a; is the syntax for the pre increment operator.
Post increment operator syntax: X = a+++;
X = --a; is the syntax for the pre decrement operator.
X = --; syntax for the post decrement operator
Each operator only applies to a single operand, not to values. Each operator only applies to a single operand, not to values.

Conclusion

Increment and decrement operators in C are instrumental in manipulating variables and running loops. They allow for quick and easy arithmetic operations and can significantly reduce the amount of code written. The increment operator (++) adds one to the value of a variable, whereas the decrement operator (--) subtracts one from the value of a variable. These operators are commonly used for loops to increment or decrement the loop counter by one each time the loop is executed. This behavior is useful when you need to track the number of times a loop has been executed. By using increment and decrement operators, you can simplify your code and make it easier to read and understand.

We hope you like this article. We have begun with a quick overview of increment and decrement operators in C. Please let me know in the comment section if you have any trouble keeping up. Happy studying!

Related Questions

1. What does I ++ mean in C?

i++ increment the variable i by 1. It is the same as I = I + 1. I decreases the value of the variable I by 1.

2. What is the difference between += and =+ in programming?

In current or even fairly old C, += is a compound assignment operator, while =+ is parsed as two distinct tokens. = and + . It is permitted for punctuation tokens to be contiguous.

3. What is diff between i ++ and ++ i?

The only difference between the variable increment and the value returned by the operator is the order of operations. i++ returns the value prior to incrementing it, while I returns the value after incrementing it. In both circumstances, the I variable's value will be increased at the conclusion.

4. Is ++ I the same as I += 1?

These two items are similar. It's just two distinct methods of expressing the same concept. i++ is an abbreviation for i += 1, which is an abbreviation for i = i + 1.



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.