Using Increment and Decrement Operators in C: A Beginner's Guide


C has two special unary operators called increment (++)and decrement (--) operators. These operators increment and decrement value of a variable by 1.

  1. Unary Increment Operator, denoted by "++"
  2. Unary Decrement Operator, denoted by "--"

Post-Increment : Value is first processed then incremented. In post increment, whatever the value is, it is first used for computing purpose, and after that, the value is incremented by one.

Pre-Increment : On the contrary, Pre-increment does the increment first, then the computing operations are executed on the incremented value.

Post-Decrement : While using the decrement operator in post form, the value is first used then updated.

Pre-Decrement : With prefix form, the value is first decremented and then used for any computing operations.

The code is a C program that demonstrates the use of increment and decrement operators in C. Here is an explanation of each line of the code:

  • int a=1; declares a variable a of type int and assigns it the value 1.
  • printf("\nPre Increment : %d",++a); uses the pre-increment operator '++' to increment the value of a by 1 and then the new value of a is printed in the console.
  • printf("\nPost Increment : %d",a++); uses the post-increment operator '++' to print the current value of a and then increment the value of a by 1
  • printf("\nA : %d",a); prints the final value of a which is 3, this is the result after using pre and post increment operator
  • printf("\nPre Decrement : %d",--a); uses the pre-decrement operator '--' to decrement the value of a by 1 and then the new value of a is printed in the console.
  • printf("\nPost Decrement : %d",a--); uses the post-decrement operator '--' to print the current value of a and then decrement the value of a by 1
  • printf("\nA : %d",a); prints the final value of a which is 1, this is the result after using pre and post decrement operator
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned as the exit status of the program, which indicates a successful execution.

When you run this code, it will perform increment and decrement operations on variable a and print the results in the console.

It's important to note that pre-increment/decrement operator will increment/decrement the value of the variable first and then use it while post-increment/decrement operator will use the current value of the variable and then increment/decrement it.

Source Code

//Increment and Decrement Operators
 
#include<stdio.h>
int main()
{
    int a=1;
    printf("\nPre Increment  : %d",++a);
    printf("\nPost Increment : %d",a++);
    printf("\nA : %d",a);//3
    printf("\nPre Decrement  : %d",--a);
    printf("\nPost Decrement  : %d",a--);
    printf("\nA : %d",a);
    return 0;
}
 
 
To download raw file Click Here

Output

Pre Increment  : 2
Post Increment : 2
A : 3
Pre Decrement  : 2
Post Decrement  : 2
A : 1

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C