View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
  1. Home
c tutorial

Explore C Tutorials: From Beginner Concepts to Advanced Techniques

Dive into our C tutorials and expand your programming skills. From fundamental concepts to advanced techniques, we've got you covered. Start your journey to C mastery today

  • 130 Lessons
  • 22 Hours
right-top-arrow

Tutorial Playlist

132 Lessons
74

Increment and decrement operators in c

Updated on 01/03/20243,116 Views

Overview

C language provides a multitude of operators, each designed to manipulate and transform data. Among these, the increment and decrement operators in C are a fundamental part of the language, vital for the control of loops, and sometimes used for fine-tuning arithmetic computations.

Increment and decrement operators refer to unary operators that function by respectively adding or subtracting one, to or from their operand. These are most commonly used in 'for' loops and 'while' loops. Before we delve deeper into these operators, it's important to note that there are two types of these operators: prefix and postfix.

Types of Increment and Decrement Operators in C

Increment Operators in C

Prefix Increment Operator

Syntax of Prefix Increment Operator

The prefix increment operator in C (++) increases the variable’s value by one before the operation is carried out. The general form is:

++variable;
Example of Prefix Increment Operator

Let's consider a simple explanation of one of the increment operator in C examples.

#include <stdio.h>

int main() {
   int a = 5;

   printf("Value of a before increment: %d\n", a);
   printf("Value of a after prefix increment: %d\n", ++a);

   return 0;
}

In the above program, the prefix increment operator is used. The variable a is first incremented and then printed. So, the output will be -

Value of a before increment: 5
Value of a after prefix increment: 6

Postfix Increment Operator

Syntax of Postfix Increment Operator

The postfix increment operator (++) increases the variable’s value by one after the operation is carried out. The general form is -

variable++;
Example of Postfix Increment Operator

Consider the following example.

#include <stdio.h>

int main() {
   int a = 5;

   printf("Value of a before increment: %d\n", a);
   printf("Value of a after postfix increment: %d\n", a++);

   return 0;
}

In this case, a is printed first, then incremented. Therefore, the output will be -

Value of a before increment: 5
Value of a after postfix increment: 5

Decrement Operators in C

Prefix Decrement Operator

Syntax of Prefix Decrement Operator

The prefix decrement operator (--) decreases the variable’s value by one before the operation is carried out. The general form is -

--variable;
Example of Prefix Decrement Operator

Let's consider an example.

#include <stdio.h>

int main() {
   int a = 5;

   printf("Value of a before decrement: %d\n", a);
   printf("Value of a after prefix decrement: %d\n", --a);

   return 0;
}

Here, the value of a is decremented before it is printed. So, the output will be -

Value of a before decrement: 5
Value of a after prefix decrement: 4

Postfix Decrement Operator

Syntax of Postfix Decrement Operator

The postfix decrement operator (--) decreases the variable’s value by one after the operation is carried out. The general form is -

variable--;
Example of Postfix Decrement Operator

Here's an example.

#include <stdio.h>

int main() {
   int a = 5;

   printf("Value of a before decrement: %d\n", a);
   printf("Value of a after postfix decrement: %d\n", a--);

   return 0;
}

In this case, a is printed first, then decremented. Therefore, the output will be -

Value of a before decrement: 5
Value of a after postfix decrement: 5

Precedence in Increment and Decrement Operators in C

In any programming language, operators have a certain priority level which is known as precedence. In the case of the increment (++) and decrement (--) operators in C, they have one of the highest levels of precedence, right after the scope resolution operator, function call, array subscript, and member access operators. This high precedence is applicable to both prefix and postfix versions of these operators.

However, the timing of the operation differs between the prefix and postfix forms. Prefix increment/decrement (++a or --a) performs the operation before the value is implemented in the expression, while postfix increment/decrement (a++ or a--) performs the operation after the value is used in the expression.

This difference becomes crucial when these operators are used in more complex expressions.

Consider the following examples -

Example 1: Prefix increment operator

#include <stdio.h>

int main() {
   int a = 5, b;
   
   b = ++a * 3;

   printf("Value of a: %d\n", a);
   printf("Value of b: %d\n", b);

   return 0;
}

In this case, a is first incremented to 6, and then the multiplication is carried out, resulting in b being 18. So, the output will be -

Value of a: 6
Value of b: 18

Example 2: Postfix increment operator

#include <stdio.h>

int main() {
   int a = 5, b;
   
   b = a++ * 3;

   printf("Value of a: %d\n", a);
   printf("Value of b: %d\n", b);

   return 0;
}

In this case, the multiplication is carried out first, and then a is incremented. So, b becomes 15, and a becomes 6. Therefore, the output will be -

Value of a: 6
Value of b: 15

In both examples, the increment operator has the highest precedence in the expression, but the time at which the increment operation is performed varies depending on whether it is a prefix or postfix.

Increment Vs Decrement: Differences

While the increment operator extends the value of its operand by one, the decrement operator lessens it by one. The difference between prefix and postfix operators lies in the timing of their operation in an expression - prefix operators change the operand before its value is used in the expression, while postfix operators change the operand after its value is used in the expression.

Here are the differences between prefix and postfix increment and decrement operators in a tabular form:

Prefix Increment (++a)

Postfix Increment (a++)

Prefix Decrement (--a)

Postfix Decrement (a--)

Operation

Increases a by 1 before a is used in the expression

Increases a by 1 after a is used in the expression

Decreases a by 1 before a is used in the expression

Decreases a by 1 after a is used in the expression

Use Case

When you want to increment the value of a before it is used in the rest of the expression

When you want to increment the value of a but still use the original value in the rest of the expression

When you want to decrement the value of a before it is used in the rest of the expression

When you want to decrement the value of a but still use the original value in the rest of the expression

In essence, while both increment and decrement operators change the value of their operand, the timing of the operation can influence the outcome of the larger expression in which they are used, providing you with more control and flexibility over your computations.

Here are some of the interesting and mostly confused (or often forgotten) facts about increment and decrement operators:

  • Increment and decrement operators can only be used with variables, not constants or expressions. For example, a++ is valid, but 5++ or (a+b)++ is not.
  • Double increment or decrement like a++++ or a---- is not allowed in C.
  • Increment or decrement operator can't be applied more than once on a single variable in a single statement. For instance, a = ++a + a++; is not a valid expression.

Conclusion

Understanding the intricacies of increment and decrement operators in C is a stepping stone to mastering the language, helping you easily solve increment and decrement operators in C questions. These operators allow for more efficient code by simplifying operations and optimising loops. As we've seen, both the increment and decrement operators come in two forms, prefix and postfix, each with its own unique behaviour in the context of an expression. Getting a good grip on these differences will enable you to write more flexible and efficient code in C.

While this article provides a good starting point, nothing beats hands-on practice. To dive deeper into C and other programming languages, consider enrolling in upGrad’s MS in Full Stack AI and ML Program powered by Golden Gate University. Designed by industry professionals, this course not only covers theory but also emphasises practical application and real-world scenarios.

Enroll today and take a significant step towards enhancing your programming skills!

FAQs

  1. What is the difference between prefix and postfix increment and decrement operators?
    The difference lies in when the increment or decrement operation takes place. In the case of the prefix operator, the operation is done before the value is used in the expression. In contrast, the postfix operator first uses the value in the expression and then performs the operation.
  1. What are some use cases for increment and decrement operators in C?
    These operators are often used in loops to increase or decrease the loop control variable. They can also be used to traverse through an array or to implement certain algorithms more efficiently.
  1. What is the precedence of increment and decrement operators in C?
    Increment and decrement operators have high precedence. Prefix increment/decrement operators are evaluated before other operations, while postfix increment/decrement operators are evaluated after other operations.

Test your C programming skills with a Free Quiz

image
right-top-arrowright-top-arrow
Need Guidance? We're Here to Help!
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. .