Right Click to Search

Monday, June 21, 2024

Typical Example : Post-Increment and Pre-Increment Expression in Same Statement


Postfix and Prefix Expression in Same Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0;
j = i++ + ++i;
printf("%d\n",i);
}
Output :
2
Explain ?
j = i++ + ++i;
  1. Above Statement Contain in all 4 Operators , [ = , Post Increment , Pre Increment , + ]
  2. According to Precedence there Order is -
OperatorPrecedense Rank
Pre Increment1
Post Increment2
+3
=4
  1. It is very interesting to know that , Which one gets executed first Pre-Increment , Post-Increment ? Refer Operator Associativity  Unary Operator's associativity is from [ R->L] so PreIncrement Operation gets executed first
  2. Here Pre Increment Operation uses new Value o 'i' so [ i = 1 ].
  3. This 'i = 1' is used for Post Increment Operation which uses Old Value i.e [i = 1] .
  4. Both '1's are added and value 2 is assigned to 'j'.

Tags / Keywords : | , ,

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates