C Program to Demonstrate Increment and Decrement Operators

Write C Code to demonstrate increment and decrement operators

//Write a program to demonstrate increment and decrement operator

#include <stdio.h>

int main()
{
    int a = 6, ans;

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

    ans = a++;      //post increment
    printf("The value a++ : %d",ans);
    //a is updated after execution of ans

    printf("\n\nValue of a :--> %d", a);
    ans = a--;    //post decrement
    printf("\nThe value a-- : %d",ans);
    //a is updated after execution of ans

    printf("\n\nValue of --a :--> %d", a);
    ans = --a;   //pre decrement
    //a is updated before execution of the ans
    printf("\nThe value of --a : %d",ans);

    printf("\n\nValue of a :--> %d", a);
    ans = ++a;   //pre increment
    //a is updated before execution of the ans
    printf("\nThe value of ++a : %d",ans);

    return 0;
}

Output:

Value of a :–> 6
The value a++ : 6

Value of a :–> 7
The value a – – : 7

Value of a :–> 6
The value of – – a : 5

Value of a :–> 5
The value of ++a : 6


<< Previous | Next >>


  • C++ Program to Demonstrate Increment and Decrement Operators
  • Java Program to Demonstrate Increment and Decrement Operators
  • Python Program to Demonstrate Increment and Decrement Operators