Write a C program to demonstrate post increment and pre increment operators


Increment operator (++)

  • It is used to increment the value of a variable by 1. There are two types of increment operators, pre-increment and post-increment.

  • The increment operator is placed before the operand in pre-increment and the value is first incremented and then the operation is performed on it.

For example,

z = ++a; a= a+1
z=a
  • The increment operator is placed after the operand in post-increment and the value is incremented after the operation is performed.

For example,

z = a++; z=a
a= a+1

Example 1

Following is an example for pre-increment operator −

 Live Demo

main ( ){
   int A= 10, Z;
   Z= ++A;
   printf ("Z= %d", Z);
   printf (" A=%d", A);
}

Output

Z =11
A=11

Example 2

Following is an example for post-increment operator −

 Live Demo

main ( ){
   int a= 10, z;
   z= a++;
   printf ("Z= %d", z);
   printf ("A=%d", a);
}

Output

Z=10
A=11

Decrement operator (- -)

  • It is used to decrement the values of a variable by 1. There are two types of increment operators, pre decrement and post decrement.

  • The decrement operator is placed before the operand in pre decrement and the value is first decremented and then the operation is performed on it.

For example,

z = - - a; a= a-1
z=a
  • The decrement operator is placed after the operand in post decrement and the value is decremented after the operation is performed

For example,

z = a--; z=a
a= a-1

Example 1

Following is an example for pre decrement operator −

 Live Demo

main ( ){
   int a= 10, z;
   z= --a;
   printf ("Z= %d", z);
   printf (" A=%d", a);
}

Output

Z=9
A=9

Example 2

Following is an example for post decrement operator −

 Live Demo

main ( ){
   int a= 10, z;
   z= a--;
   printf ("Z= %d", z);
   printf ("A=%d", a);
}

Output

Z=10
A=9

Updated on: 06-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements