Skip to content
HOME - C++ - Increment and decrement operator in c programming

Increment and decrement operator in c programming

c language increment and decrement operator

The increment operator is used to increase and the decrement operator is used to decreasing the value of a variable by one.

Increment operator (++)

The increment operator is a unary operator that is used to increase the value of variables by one. ++ is used as an increment operator. Only the variables can be incremented but it can’t be used with constants and expressions. It can be used before or after the variable name. For example, x is the variable then the increment operator can be used in this way x++ or ++x. There are two types of increment operators.

1. Prefix increment operator

When ++ precedes its operand (variable), it is called the prefix increment operator. In this case, ++ increases the value of the variable before execution of the statement i.e, it first increases the current value of the variable by one, and then a new value which is incremented by one is used in the statement.

Example
x = 50;
y = ++x; // x=51, y=51

2. Postfix Increment Operator

When ++ follows its operand (variable), it is called the postfix increment operator. In this case, ++ increases the value of the variable after the execution of the statement. It first uses the current value of the variable in the statement and after the completion of the statement, it increases the value by one.

Example
x = 50;
y = x++; // x=51, y=50

Decrement operator (- -)

Decrement operator is also a unary operator that is used to decrease the value of variables by one. – – is used as a decrement operator. It can also be used before or after the variable name for example x- – or – -x. Like the increment operator, it is only applicable with variables but does not work with constants and expressions. There are two types of decrement operators.

1. Prefix decrement operator

When – – preceded its operand (variable), it is called prefix decrement operator. In this case, – – decreases the value of the variable by one before the execution of the statement. It first decreases the current value of the variable by one and then a new value is used in the statement.

Example
x = 10;
y = – -x; // x=9, y=9

2. Postfix decrement operator

When – – follows its operand (variable), it is called the postfix decrement operator. In this postfix increment, – – decreases the value of a variable by one after the execution of the statement i.e, it first uses the value of the variable in the statement and after the execution of the statement, it decreases the value by one.

Example
x = 10;
y = x- -; // x=9, y=10

 

4 thoughts on “Increment and decrement operator in c programming”

  1. Pingback: What is the Assignment operator and Assignment Statement? | C++

Leave a Reply

Your email address will not be published.

en English
X