Pre Increment and Post Increment in C programming

11508

Pre Increment and Post Increment concept in C programming is very important. Here we are discussing Unary operators with example and explanations so that you can easily understand the fundamentals of Pre increment and Post increment in C programming. So Let’s start with the basics first.

In the C programming Unary operators are having higher priority than the other operators rest of the Suffix/postfix increments and decrements, Function call, Array subscripting etc. operators.  Unary operators having wide scope in loop construction with increments or decrement of a value by 1. It is very easy to use and understand C Programming operators. Complete detail about these unary operators of C language is given below with a suitable easy code examples.

There are two Unary Operators used in C Programming language;

  1. Increment and
  2. Decrements

Increment Unary Operator in C Programming:

An operator that takes the current value of the variable and adds one to them and provides a resultant value.

Properties and features of Increment unary operator:

  • An increment operator is used to increment the current value of the variable by adding an integer 1.
  • An increment operator can be applied to only variables.
  • An increment operator is denoted by ++ (double addition symbol continuously).

Types of Increment operator used in C programming:

  • Pre-increment and
  • Post-increment

Pre-increment unary operator:

Pre-increment unary operator is used to increment the value of a variable by one before using in the expression. In the Pre-Increment operator, concern value is first incremented and then it used inside the expression with final updated value.

Syntax of Pre-increment unary operator:

  • ++variable;

Example Pre-increment unary operator:

  • ++i; //it is equivalents to the i=i+1; or i+=1 ;

Programming Code 1:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d\n”,++i);
return 0;
}

Output: 6

Explanation: In the above example the value of ‘i’ is initialized by 5 after applying the pre-increment operation on variable ‘i’ then it becomes 6 and the output is printed as 6.

In other words, it is explained that if we push the expression into the stack the value of variable ‘i’ in pre-increment could not be executed until the expression completely execute. After complete execution of the expression in the stack the value of variable ‘i’ is incremented by 1 and the output of the expression is 6 because the pre-increment unary operator uses the final updated value of the stack i.e. the top value in the stack.

pre increment and post increment in c

Programming Code 2:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d , %d\n”,++i ,++i);
return 0;
}

Output: 7 , 7

Explanation: In the above example the value of ‘i’ is initialized by 5 after that applying the pre-increment operation on variable ‘i’ then it becomes 6 and once again applying the pre-increment operator it becomes i=7, the output is printed as 7 , 7 (because after complete execution of the expression the value of variable ‘i’ is 7, So both the pre-incremented variable updated at last with the updated value of variable ‘i’).

In other words, it is explained by expression execution in stack operation. We push the expression into the stack of the value of variable ‘i’ in pre-increment and could not be executed until the expression completely execute. After complete execution of the expression in the stack the value of variable ‘i’ is incremented by 1 twice and the output of the expression is 7, 7 because the pre-increment unary operator uses the final updated value of the stack i.e. the top value in the stack.

Post-increment unary operator:

In Post-increment first of all the loop executes then the value of the concern variable increments by 1 and returns the value before the present update state.

Post-increment unary operator is used to increment the value of a variable as soon as after executing the expression completely in which post-increment is used individually. In the Post-Increment, the value is first used in an expression and then incremented. In other words, Post-increment unary operator returns the value before the present state updation.

Syntax of Post-increment unary operator:

  • variable++;

Example Post-increment unary operator:

  • ++i;

Programming Code 1:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d\n”,i++);
return 0;
}

Output: 5

Explanation: In the above example the value of ‘i’ is initialized by 5 after applying the post-increment operation on variable ‘i’ then it is updated 6 but the output is printed before the update i.e. 5.

 

Programming Code 2:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d , %d\n”,i++ , i++);
return 0;
}

Output: 6 , 5

Note: The execution of Unary Operator (Post-increment or Pre-increments execute form Right to left).

Hope this will be useful for you – But It’s not just End Here is a Quiz on this Pre Increment and Post Increment in C programming – Attempt Now

Attempt Free C Programming MCQ Quiz

Previous QuizWhat is Robot-A brief Introduction
Next QuizPhysics Timeline

1 COMMENT

  1. Thank You ,sir I read continuesly and we understand…
    I hope that you understand mine easy to Way……

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.