Difference between Pre Increment and Post Increment

Pre Increment and Post Increment are the features of unary operators. Unary operators are the important concepts of programming and is very easy to learn. Unary operators are mostly used to looping statements, function calls, etc. The unary operators have high precedence in C compared to other operators.(click here to check precedence table). Unary operators are operators with only one operand. Two types of unary operators in C program:
1. Increment operator (++): it increases the value by one.
2. Decrement operator (--): It decreases the value by one.

Difference Between Pre Increment and Post Increment.

Pre Increment Post Increment
It is used to increase the value of the variable by one before using in the expression It is used to increase the value of the variable by one only after using in the expression
It value gets incremented as soon as the expression is used The original value is used in an expression and then only it is incremented.
Its syntax is : ++ variable; Its syntax is : variable ++;
code snippet:
#include <stdio.h>
#include <stdlib.h>

void main()
{
   
   int res, n;

   n = 7;

   res = ++n;
   
   printf("%d", res);
}
Output: 8
code snippet:
#include <stdio.h>
#include <stdlib.h>

void main()
{
   
   int res, n;

   n = 7;

   res = n++;
   
   printf("%d", res);
}
Output: 7

2 comments: