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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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