Increment Decrement Operators in C (++ / –)

Preview

1. Increment Operator (++)

Increment operator is represented as ++. This operator is used to add one to the existing value of a variable.

The variable can be of any data type including integer, real as well as character data type. It can be used in two ways:-

Prefix Form

In this form, the increment operator is placed before the operand.

Example: ++a.

It first adds one to the existing value of variable then takes the incremented value of variable for further use in the program.

Postfix Form

In this form, the increment operator is placed after the operand.

Example: a++.

It first takes the existing value of variable and then adds one to the existing value of variable in the next statement for further use in the program.



2. Decrement Operator (–)

Decrement operator is represented as –. This operator is used to subtract one from the existing value of a variable.

The variable can be of any data type including integer, real as well as character data type. It is can be used in two ways:-

Prefix Form

In this form, the increment operator is placed before the operand.

Example: –a

It first subtracts one from the existing value of variable then takes the decremented value of variable for further use in the program.

Postfix Form

In this form, the decrement operator is placed after  the operand.

Example: a–

It first takes the existing value of variable and then subtracts one from the existing value of variable in the next statement for further use in the program.

Example 1

Program to demonstrate increment / decrement operator.

 

 Output (Line Wise)

#include<stdio.h>
#include<conio.h>
int  main()
{
int a=10;

 

 

printf(“\na=%d”,++a);a=11
printf(“\na=%d”,a++);a=11
printf(“\na=%d”,a);a=12
printf(“\na=%d”,a–);a=12
printf(“\na=%d”,–a);a=10
getch();
return (0);
}
 

Example 2

Program to demonstrate

increment/decrement operator

 Output (Line Wise)

#include<stdio.h>
#include<conio.h>
int  main()
{
int a=10;
clrscr();

Description of Output

In Line 8 below, a++ is evaluated first followed by ++a. Because if more than one argument appears in printf() function, arguments are evaluated from right towards left.

/*Line 8*/
printf(“\n%d,%d”,++a,a++);
12,10
printf(“\n%d,%d”,a–,a–);11,12
printf(“\n%d”,a);10
getch();
return (0);
}

 

Lesson tags: decrement operator in c, increment operators in C, postfix form of decrement operator, postfix form of increment operator, prefix form of decrement operator, prefix form of increment operator
Back to: C Programming Language