How to evaluate multiple increment operators inside printf ?
Printing Variable having Different Form :
#include<stdio.h> void main() { int i=1; printf("%d %d %d",i,++i,i++); }
Output :
3 3 1
How ?
- Whenever more than one format specifier [ %d Here] are directly or indirectly related with same variable [i,i++,++i] then firstly evaluate each individual expression in [ <----- ] Direction.
- So Sequence of Evaluation will be -
- i++
- ++i
- i
- Evaluating i++ : While Evaluating [i++] always old value of ” variable i ” is used and new value is used thereafter .[ Here Old value is 1 and new incremented value is 2 ]
- Evaluating ++i : While Evaluating [++i] always new value of ” variable i ” is used and old value is discarded .[ Here Old value is 2 and new incremented value is 3 ]
- Evaluating i : Directly Printing ‘i’
Printing Sequence :
- i
- ++i
- i++