Home » Pre-increment and Post-increment Operator in C

Pre-increment and Post-increment Operator in C

by Online Tutorials Library

Pre-increment and Post-increment Operator in C

Increment operators are the operator of the C programming language used to increase the given variable’s value by 1. The increment operator can increase the given value by 1 before assigning it to the variable. On the other hand, the increment operator can increase the given value by 1 after assigning the variable. The increment operator is represented as the double plus (++) symbol, which means the value is incremented by 1.

Pre-increment and Post-increment Operator in C

Properties of the Increment operator

There are some properties of the increment operator, as follows:

  1. The increment operator is used to increase the current value of the variable by 1.
  2. We can only use these operators with variables.
  3. It is the operator represented by the double plus (++) symbol.

There are two types of the increment operators

  1. Pre-increment operator
  2. Post-increment operator

Pre-increment operator

The pre-increment operator is represented as the double plus (++a) symbol, appended before the variable’s name. The pre-increment operator is used to increment the value of an operand by 1 before using it in the mathematical expression. In other words, the value of a variable is first incremented, and then the updated value is used in the expression.

Syntax

In the above syntax, the value of variable ‘a’ is first incremented by 1 before using in the expression.

Example 1: Let’s create a simple program to use the pre-increment operator in C programming language.

Program1.c

Output

Use the pre-increment operator  The value of a is 7  The value of b is 2  After using the pre-increment operator  The value of a is 8  The value of b is 10  

Example 2: Let’s create another program to use the pre-increment operator in mathematical expression.

Program2.c

Output

The value of x is: 43   The updated value of a = 6, b = 8, c = 13 and d = 16  

Post-increment Operator

Post-increment is an increment operator, represented as the double plus (a++) symbol followed by an operator ‘a’. It increments the value of the operand by 1 after using it in the mathematical expression. In other words, the variable’s original value is used in the expression first, and then the post-increment operator updates the operand value by 1.

Syntax

In the above syntax, the operand ‘a’ value is assigned to the variable x, and then the post increment operator increases or updates the value of ‘a’ by 1.

Example 1: Let’s create a simple program to use the post-increment operator in C programming language.

Program1.c

Output

Before using the post-increment operator   The value of a is 7   The value of b is 0     After using the post-increment operator   The value of a is 8   The value of b is 7  

Example 2: Let’s create another program to use the post-increment operator in mathematical expression.

Program2.c

Output

The value of x is: 39   The updated value of a = 6, b = 8, c = 13 and d = 16  

Program to use the Pre-increment and Post-increment Operator

Let’s create a simple program to use the pre-increment and post-increment operator in the C programming language.

Program3.c

Output

Enter the value of x: 7     Enter the value of y: 12     Enter the value of z: 15     Before using the increment operator:   The original value of x: 7   The original value of x: 12   The original value of x: 15     After using the increment operator:   The result of the expression is: 58   The updated value of x: 9   The updated value of y: 14   The updated value of z: 16  

You may also like