×

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

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 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.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.