What does increment and decrement operator mean?

The decrement (–) and increment (++) operators are special types of operators used in programming languages to decrement and increment the value of the given variable by 1 (one), respectively.

In this article, we will dig deeper into Increment and Decrement Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

  • Types Of Increment And Increment In C
  • Increment Operators
    • Prefix Increment Operators
    • Postfix Increment Operators
      • Syntax Of Increment Operators
      • Example Of Increment Operators
  • Decrement Operators
    • Prefix Decrement Operators
    • Postfix Decrement Operators
      • Syntax Of Increment Operators
      • Example Of Decrement Operators
  • Precedence In Increment And Decrement Operators In C
  • Practice Problems On Increment And Decrement Operators In C
  • Faqs

Types of Increment and Decrement Operators in C

Following types of increment and decrement operators are found in the C language:

  • Prefix Increment operator
  • Prefix Decrement operator
  • Postfix Increment operator
  • Postfix Decrement operator

Both the operators vary a lot in their fundamental working. However, we can only use these with variables, and not with expressions or constants. Read more about the difference between Increment and Decrement Operators.

Increment Operators

We use increment operators in C to increment the given value of a variable by 1.

For instance,

int a = 1, b = 1;

++b; // valid

++3; // invalid – increment operator is operating on constant value

++(a+b); // invalid – increment operator is operating on expression

Prefix Increment Operators

The prefix increment operator increments the current value of the available variable immediately, and only then this value is used in an expression. In simpler words, the value of a variable first gets incremented and then used inside any expression in the case of an increment operation.

b = ++a;

In this case, the current value of a will be incremented first by 1. Then the new value will be assigned to b for the expression in which it is used.

Thus, Syntax for Prefix Increment Operator:

++variable;

For example,

#include

#include

void main()

{

int p,q;

i=10;

p=++q;

printf(“p: %d”,p);

printf(“q: %d”,q);

getch();

}

The output here would be:

p: 11

q: 11

Postfix Increment Operators

The postfix increment operator allows the usage of the current value of a variable in an expression and then increments its value by 1. In simpler words, the value of a variable is first used inside the provided expression, and then its value gets incremented by 1.

For example:

b = a++;

In this case, the current value of a is first assigned to b, and after that, a is incremented.

Thus, Syntax for Postfix Increment Operator:

variable++;

For example,

#include

#include

void main()

{

int a,b;

b=10;

a=b++;

printf(“a: %d”,a);

printf(“b: %d”,b);

getch();

}

The output here would be:

a: 10

b: 11

Syntax of Increment Operators

// PREFIX

++x

// POSTFIX

x++

where x is a variable

Example of Increment Operators

#include

int main() {

int x = 5, y = 5;

// x is displayed

// Then, x is increased to 6.

printf(“%d post-increment n”, x++);

// y is increased to 6

// Then, it is displayed.

printf(“%d pre-incrementn”, ++y);

return 0;

}

The output here would be:

5 post-increment

6 pre-increment

Decrement Operators

We use decrement operators in C to decrement the given value of a variable by 1.

For instance,

int a = 2, b = 1;

–b; // valid

–5; // invalid – decrement operator is operating on constant value

–(a-b); // invalid – decrement operator is operating on expression

Prefix Decrement Operators

The prefix decrement operator decrements the current value of the available variable immediately, and only then this value is used in an expression. In simpler words, the value of a variable first gets decremented and then used inside any expression in the case of a decrement operation.

b = –a;

In this case, the current value of a will be decremented first by 1. Then the new value will be assigned to b for the expression in which it is used.

Thus, Syntax for Prefix Decrement Operator:

–variable;

For example,

#include

#include

void main()

{

int p,q;

i=10;

p=–q;

printf(“p: %d”,p);

printf(“q: %d”,q);

getch();

}

The output here would be:

p: 9

q: 9

Postfix Decrement Operators

The postfix decrement operator allows the usage of the current value of a variable in an expression and then decrements its value by 1. In simpler words, the value of a variable is first used inside the provided expression, and then its value gets decremented by 1. For example:

b = a–;

In this case, the current value of a is first assigned to b, and after that, a is decremented.

Thus, Syntax for Postfix Decrement Operator:

variable–;

For example,

#include

#include

void main()

{

int a,b;

b=10;

a=b–;

printf(“a: %d”,a);

printf(“b: %d”,b);

getch();

}

The output here would be:

a: 10

b: 9

Syntax of Increment Operators

// PREFIX

–x

// POSTFIX

x–

where x is a variable

Example of Decrement Operators

#include

int main() {

int x = 5, y = 5;

// x is displayed

// Then, x is decreased to 4.

printf(“%d post-decrement n”, x–);

// y is decreased to 4

// Then, it is displayed.

printf(“%d pre-decrement”, –y);

return 0;

}

The output here would be:

5 post-decrement

4 pre-decrement

Precedence in Increment and Decrement Operators in C

Both of these operators have very high precedence (parentheses are an exception). Also, the precedence of postfix decrement/ increment operators is higher than that of prefix decrement/ increment operators.

Here is a table that discusses the precedence associativity of these operators:

DescriptionOperatorsAssociativitypostfix decrement operator—left to rightpostfix increment operator++left to rightparentheses()left to rightprefix increment operator++right to leftprefix decrement operator—right to leftunary plus+right to leftunary minus–right to leftMultiplication*left to rightDivision/left to rightModulus%left to rightAddition+left to rightSubtraction–left to rightAssignment Operator=right to leftCompound Assignment Operator=, +=, -=, *=, /=, %=right to left

Practice Problems on Increment and Decrement Operators in C

1. Choose the correct output for the C program mentioned below:

#include

int main()

{

int a, b;

a = 5;

b = x++ / 2;

printf(“%d”, y);

return 0;

}

A. 3

B. 2

C. Compile-time error

D. None of these

Answer – B. 2

The expression given here is:- b=a++ / 2; a++ thus post-increment, b=5/2=2. Hence, a=6 and b=2.

2. Choose the correct output for the C program mentioned below:

#include

int main()

{

int x=4,y,z;

y = –x;

z = x–;

printf(“%d %d %d”,x,y,z);

return 0;

}

A. 3 2 2

B. 2 3 2

C. 3 3 2

D. 2 3 3

Answer – D. 2 3 3

The first expression given here is y=–x; thus, x becomes 3 (x=3) and y=3. Now, z=x–; so, z=3 and x=2. Finally, x=2, y=3, and z=3.


FAQs

What would be the output if we use the ++a++ in a code?

We will receive a Compile time error. It is because an lvalue is required here as an increment operand.

What would be the output if we use the ++(a*b+1) in a code?

We will receive a Compile time error. It is because an lvalue is required in this case as well in the form of an increment operand. Also, the operand of decrement and increment operators must not be an expression or a constant. It should rather be a variable.

Can we use prefix and postfix operators in the same code with both – increment and decrement operators?

Yes, we can. Refer to the aforementioned examples to understand how they work when combined together

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

What is meant by increment and decrement operator?

Increment and decrement operators are unary operators that add or subtract one, to or from their operand, respectively. They are commonly implemented in imperative programming languages. C-like languages feature two versions (pre- and post-) of each operator with slightly different semantics.

What is i ++ and ++ i explain with an example?

2) Pre-Increment(++i): We use ++i in our statement if we want to increment the value of i by 1 and then use it in our statement. Example int i = 3; int a = i++; // a = 3, i = 4 int b = ++a; // b = 4, a = 4.

What does i ++ and I

i++ increment the variable i by 1. It is the equivalent to i = i + 1. i– decrements (decreases) the variable i by 1.

What is the meaning of decrement operator?

What Does Decrement Operator Mean? A decrement operator, in the context of C#, is a unary operator. It returns a value of same type, with predefined value equal to the operand value minus one. The decrement operator is denoted by the symbol '—'. A decrement operator supports both prefix and postfix notations.