Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Pre Increment and Post Increment in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hello Ninjas, Welcome back! C is a general-purpose programming language developed by Dennis Ritchie in the 1970s. C is a very efficient and user-friendly language. It has inspired the development of languages like C++, an improvised version of C.

pre increment and post increment in c


The increment operators in C are used to increment to increase the value of any variable by one. There are mainly two main types of increment operators: pre-increment and post-increment. This article will discuss pre increment and post increment in C and how they are implemented. So let's begin with understanding the basic syntax and meaning of the two operators in C.

Properties of the Increment Operator

The properties of the increment operator are mentioned below:

  • Increases the variable by 1
     
  • Operates on a single operand
     
  • Modifies the variable it operates on
     
  • Post-increment uses the current value first, then increments
     
  • Pre-increment increments first, then use the updated value
     
  • Works with integers and floating-point numbers
     
  • Used in loops and iterative processes for variable updates
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is a Pre-increment Operator?

The pre-increment operator in C is used to increment the value of any variable by one before assigning it to another variable. It is denoted as "++." 

Syntax

/*Syntax of the pre-increment operator*/
x = ++y;


The variable "x" in the syntax above refers to the variable we need to increment. Given below is the implementation of a pre-increment operator in a C program. Let's take a look to understand better -  
 

  • C

C

/*implementation of pre-increment operator*/
#include<stdio.h>
void main()
{
int var=10,temp;
temp = ++var;
printf(“Value of var:%d\n”, var);
printf(“Value of temp:%d\n”, temp);
}

Output

Output

Explanation 

In this code, we are using the pre-increment operator. The value of variable "var" is incremented by one before being assigned to the variable temp.
 

Also read,  Short int in C Programming

What is a Post-increment Operator?

The post-increment operator in C is used to increment the value of any variable by one after assigning it to another variable. It is denoted as "++." 

Syntax

/*Syntax of the post-increment operator*/
x = y++;


The variable "x" in the syntax above refers to the variable we need to increment. Given below is the implementation of a pre-increment operator in a C program. Let's take a look to understand better -- 

  • C

C

/*implementation of post-increment operator*/
#include<stdio.h>
void main()
{
int var=10,temp;
temp = var++;
printf(“Value of var:%d\n”, var);
printf(“Value of temp:%d\n”, temp);
}

Output

output

Explanation

In this code, we use the post-increment operator on the variable "var"; the variable on the left side of the assignment operator is assigned as equal to the value of "var," and then the value in “var” is incremented by one.

Also see, Floyd's Triangle in C

Special case for Post-increment Operator

Now that we have gathered a basic understanding of what the pre- and post-increment operators in C are, do you wonder what will happen if we assign the post-incremented value of a variable to the same variable? The answer is that the value of the variable remains the same. Let's take a look at its syntax given below -  
 

/*Syntax for the special case for post-increment operator*/
x = x++;


The term “x” in the syntax above refers to the variable we need to increment. Let’s take a look to understand better - 
 

  • C

C

#include<stdio.h>
void main()
{
int var=6;
printf("Value of var: %d \n", var);
var=var++;
printf("Value of var after post increment: %d \n", var);
}

Output

output

Explanation 

In this code, the post-increment operator is used, and the value of variable "var" is incremented by one after the assignment.

Evaluation of Pre-increment and Post-increment Operators in C

The precedence of the post-increment operator in C is greater than that of the pre-increment operator. Moreover, the associativity of the pre-increment operator in C is from right to left, while that of the post-increment operator is from left to right. Let's look at the example given below to understand the evaluation of the pre increment and post increment in C -
 

  • C

C

#include<stdio.h>
void main()
{
int num1=6;
int num2=5;
int num3=++num1 + num2++;
printf("Value of num1: %d \n", num1);
printf("Value of num2: %d \n", num2);
printf("Value of num3: %d \n", num3);
}


Output

Output

Explanation

In this code, both the pre-and post-increment operators are used. The value of "num1" is initially six, and "num2" is 5. The value of "num3" is calculated by performing pre-increment on "num1" and post-increment on "num2". 

Must Read Passing Arrays to Function in C

Frequently Asked Questions

What is ++ I and I ++ in C?

In C programming language, ++ is the increment operator. It is used to increase the value of a variable by 1. We can write ++i(Pre increment) or i++(Post increment). By pre increment, we can increment the value of i by 1 before using the updated value in the current expression. On the other hand, by post increment, we can increment the value of i by 1 after using its current value in the current expression.

What is the difference between post and pre decrement in C?

In C, post-decrement uses the current value, then decrements. On the other hand, pre-decrement decrements first and returns the updated value.

What is the use of the pre-increment operator in C?

The pre-increment operator in C is used to increment the value of any variable by one before assigning it to another variable.     

What is the use of the post-increment operator in C?

The post-increment operator in C is used to increment the value of any variable by one after assigning it to another variable.     

What is the difference between pre-and post-increment operators in C?

The pre-increment operator in C is used to increment the value of any variable by one before assigning it to another variable. In contrast, the post-increment operator in C is used to increment the value of any variable by one after assigning it to another variable.     

Conclusion

This article discussed the pre increment and post increment in C programming language. We discussed the implementation of these operators, followed by a special case for the post-increment operator. If you want to dig deeper into C, here are some related articles -    
 

You may refer to our Guided Path on Code Studios to enhance your skill set on DSA and many more. Check out essential interview questions, practice our available mock tests, and more!

Topics covered
1.
Introduction
2.
Properties of the Increment Operator
3.
What is a Pre-increment Operator?
3.1.
Syntax
3.2.
C
3.3.
Output
3.4.
Explanation 
4.
What is a Post-increment Operator?
4.1.
Syntax
4.2.
C
4.3.
Output
4.4.
Explanation
5.
Special case for Post-increment Operator
5.1.
C
5.2.
Output
5.3.
Explanation 
6.
Evaluation of Pre-increment and Post-increment Operators in C
6.1.
C
6.2.
Output
6.3.
Explanation
7.
Frequently Asked Questions
7.1.
What is ++ I and I ++ in C?
7.2.
What is the difference between post and pre decrement in C?
7.3.
What is the use of the pre-increment operator in C?
7.4.
What is the use of the post-increment operator in C?
7.5.
What is the difference between pre-and post-increment operators in C?
8.
Conclusion