Increment and Decrement Operators in C

Increment and Decrement Operators in C

Increment and decrement operators in c; In this tutorial, you will learn about increment operator ++ and the decrement operator — in detail with the help of c programming examples.

C programming Increment and Decrement Operators

In the C programming language, there is an operator known as an increment and decrement operator which is represented by ++ and — symbols. And it is used to increase and decrease the value of the variable by 1.

There are four types of Increment and decrement operator; as shown below:

C programming Increment and Decrement Operators
++a (Increment Prefix)
–a (Decrement Prefix)
a++ (Increment Postfix)
a– (Decrement Postfix)

Syntax of C programming Increment and Decrement Operators

See the syntax of C programming pre and post Increment & Decrement Operators; as shown below:

++var a; (or) var a++;
–-var a; (or) var a–-;

Note that:- About pre & post-increment and decrement operators in C; as shown below:

  • If you use the ++ operator as a prefix like: ++var, the value of var is incremented by 1; then it returns the value.
  • If you use the ++ operator as a postfix like: var++, the original value of var is returned first; then var is incremented by 1.

The -- the operator works in a similar way to the ++ operator except -- decreases the value by 1.

Example 1 – C programming Increment and Decrement Operators

#include <stdio.h>
int main() {
   int var1 = 5, var2 = 10;

   printf("%d\n", var1++);

   printf("%d\n", var2--);

   return 0;
}

Output:

5
10

Example 2 – Pre and post increment and decrement operators in C

#include <stdio.h>
int main() {
   int var1 = 20, var2 = 40;


   //Pre increment
   printf("%d\n", ++var1);

   //Pre decrement
   printf("%d\n", --var2);

   return 0;
}

Output:

21
39

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *