Increment And Decrement Operators In C https://www.skillvertex.com/blog Fri, 10 May 2024 07:16:56 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Increment And Decrement Operators In C https://www.skillvertex.com/blog 32 32 Increment And Decrement Operators In C https://www.skillvertex.com/blog/increment-and-decrement-operators-in-c/ https://www.skillvertex.com/blog/increment-and-decrement-operators-in-c/#respond Fri, 10 May 2024 07:16:56 +0000 https://www.skillvertex.com/blog/?p=2258 Read more]]>

Table of Contents

Increment And Decrement Operators In C

In the C programming language (and in many other programming languages), the ++ operator is used for incrementing a numeric value by 1, and the -- operator is used for decrementing a numeric value by 1. These operators are often referred to as the increment and decrement operators, respectively, and they are categorized as unary operators because they operate on a single operand.

Increment Operator in C

The increment operator (++) is used to increase the value of a variable by 1 in an expression. It is a unary operator that can be applied to variables of numeric types, including integers (int), floating-point numbers (float), characters (char), and pointers, among others

Syntax of Increment Operator

// AS PREFIX
++m
// AS POSTFIX
m+

How to use the increment operator?

1. Pre-Increment

Prefix Increment (++variable): In this form, the variable is incremented by 1 first, and then the new value of the variable is used in the expression. This behavior is consistent with operator precedence rules, which dictate that the increment operation should take place before other operations involving the variable.

Example

result = ++var1;

The above expression can be expanded as

var = var + 1;
result = var;

2. Post-Increment

Post-increment, also known as postfix increment, involves using the increment operator (++) as a suffix to the operand. In this case, the increment operation is performed after all other operations involving the operand are completed. This behavior is based on operator precedence rules.

Example

result = var1++;

The above expression is equivalent

result = var;
var = var + 1;

Example of increment operator


// 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: 6
Postfix Increment: 5

Decrement Operator in C

The decrement operator (--) is used to decrease the value of a variable in an expression. Similar to the increment operator, it has both pre-decrement and post-decrement forms, each with a different order of operations.

Syntax

// AS PREFIX
--m
// AS POSTFIX
m--

M is the variable in the above syntax

1. Pre-Decrement Operator

The pre-decrement operator (--variable) decreases the value of the variable immediately when encountered, and it is indeed known as prefix decrement because the decrement operator is used as a prefix before the variable.

In the case of pre-decrement, the value of the variable is decremented before it is used in the expression, and this behavior is consistent with operator precedence rules. This ensures that the variable’s value is reduced by 1 before it’s involved in any other operations.

Example

result = --m;

It can be expanded to

m = m - 1;
result = m;

2. Post-Decrement Operator

Post-decrement (variable--) occurs when the decrement operator is used as the suffix of the variable. In this case, the decrement operation is indeed performed after all the other operators in the expression are evaluated.

In post-decrement, the current value of the variable is used in the expression, and then the variable is decremented by 1 afterward. This behavior ensures that the variable’s original value is used in the current operation before it’s decreased.

Example

result = m--;

Expression can be expanded as:

result = m;
m = m-1;

Example of Decrement Operator

// 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 = 4
Postfix = 5

Differences between Increment And Decrement Operators

Increment OperatorDecrement Operator
Increment Operator will add 1 to the operand.Whereas, the decrement Operator can subtract 1 from the operand.
In the Postfix increment operator, the expression is evaluated initially using the original value of the variable. Later, the variable is incremented(increased). The Postfix decrement operator refers to the expression that is evaluated first using the original value of the variable and then the variable is decremented(decreased).
The Prefix increment operator referred to as the variable is incremented first. The next step is to evaluate the expression using the new value of the variable. In, the Prefix decrement operator, the variable is decremented first and then the expression is evaluated using the new value of the variable.
We use this in decision-making and looping in the Increment Operator.A Decrement Operator is also used in decision-making and looping.

FAQ- Increment And Decrement Operators In C

Q1.What is an increment and decrement example?

Ans. int a = 5; ++a; – After this, a becomes 6.
int b = 10; --b; – After this, b becomes 9.
These statements increment a by 1 and decrement b by 1, respectively.

Q2. What is the name of the ++ operator?

Ans. Increment Operator ++

Q3. What is the difference between A ++ and ++ A in C?

Ans. Post-increment (a++): It increments the value of a but uses the original value of a in the current operation. The change takes effect afterward. So, if the initial value a is 2, after a++, it remains 2 in the current operation and a becomes 3 afterward.
Pre-increment (++a): It changes the value of a first and then uses that updated value in the current operation. So, if a is initially 2, after ++a, it immediately becomes 3, and the current operation uses this updated value.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/increment-and-decrement-operators-in-c/feed/ 0