×

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator that is used to increment the value of the variable in an expression.

The illustration of the increment operator is the (++) symbol which helps the value to extend by way of 1.

There are mainly two kinds of increment operators:

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

Properties of the increment operator

There are some properties of the increment operator:

  • Increment operators are used to increase the value by way of 1.
  • The increment operator solely works with variables.
  • Increment operators are represented by a double plus symbol (++).

Pre-increment Operators

As we all know, the pre-increment operators are used to increment the value by 1. In the pre-increment operator, first, we extend the value of the variable after that we use it in the expression.

The pre-increment operators are represented by a double plus symbol (++a). In the pre-increment operator, symbol usually append earlier than the variable name.

Syntax of Pre-increment operator in C

x = ++a; 

Let's take an example to understand the pre-increment operator well.

Example 1:

// program to show the usage of pre-increment operator in C 
#include <stdio.h>
int main()
{
int a, b;
//taking enter from the user
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
//printing the value of the increment operator
printf("Before the use of the pre-increment operator\n");
printf (" Before a is %d \n", a); 
printf (" Before b is %d \n", b); 
//use of pre-increment operator
b= ++a;
printf("After the usage of the pre-increment operator\n");
printf ("After a is %d \n", a); 
printf ("After b is %d \n", b); 
return 0;
}

Output:

Enter two numbers
5
9
Before the use of the pre-increment operator
 Before a is 5 
 Before b is 9 
After the usage of the pre-increment operator
After a is 6 
After b is 6

Example 2:

// program the use of the pre-increment operator in the mathematical expression in C 
#include <stdio.h>


int main()
{
int a,b,c,x;
//taking enter from the user
printf("Enter three values\n");
scanf("%d%d%d",&a,&b,&c);
//value of the variable earlier than the use of pre-increment operator
printf("Before the use of the pre-increment operator\n");
printf ("Before a is %d \n", a); 
printf ("Before b is %d \n", b); 
printf ("Before c is %d \n", c); 
//use of pre-increment operator in a mathematical expression
x= ++a + ++b + ++c;
printf("After the use of the pre-increment operator\n");
printf ("The value of x is %d \n", x); 
printf("The updated values of the variables\n");
printf ("The updated value of a is %d \n", a); 
printf ("The updated value of b is %d \n", b); 
printf ("The updated value of c is %d \n", c); 
return 0;
}

Output:

Enter three values
2
4
6
Before the use of the pre-increment operator
Before a is 2 
Before b is 4 
Before c is 6 
After the use of the pre-increment operator
The value of x is 15 
The updated values of the variables
The updated value of a is 3 
The updated value of b is 5 
The updated value of c is 7

Post-increment Operators

The post-increment operator is used to increment the value by means of 1 after the use of any expression. In different words, in the post-increment operator, we use value first in the expression after that we increment the value.
The post-increment operator is represented by way of a double plus symbol (a++). Basically, post-increment is used to increment the value of the operand by using 1 after the use of in the mathematical expression.

Syntax of Post-increment operator in C

A == b++

Let's take an example to explain the post-increment operator well:
Example 1:

// program to show the usage of post-increment operator in C 
#include <stdio.h>
int main()
{
int a,b;
//taking enter from the user
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
//printing the value of the increment operator
printf("Before the use of the post-increment operator\n");
printf ("Before a is %d \n", a); 
printf ("Before b is %d \n", b); 
//use of post-increment operator
b= a++;
printf("After the use of the post-increment operator\n");
printf ("Updated value of a is %d \n", a); 
printf ("Updated value of b is %d \n", b); 
return 0;
}

Output:

Enter two numbers
5
7
Before the use of the post-increment operator
Before a is 5 
Before b is 7 
After the use of the post-increment operator
Updated value of a is 6 
Updated value of b is 5

Related Topics

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.