Saturday, July 14, 2012

C Evaluation Order Questions

1.void main(){
  int i=4,x;
  x=++i + ++i + ++i;
  printf("%d",x);
}
In ++a, ++ is pre increment operator. In any mathematical expression pre increment operator first increment the variable up to break point then starts assigning the final value to all variable.
Step 1: Increment the variable I up to break point.


Step 2: Start assigning final value 7 to all variable i in the expression.



So, i=7+7+7=21
2.void main(){
  int a=10;
  printf("%d %d %d",a,a++,++a);
} 
In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction.


So first ++a will pass and value of variable will be a=10 then a++ will pass now value variable will be a=10 and at the end a will pass and value of a will be a=12. 

No comments:

Post a Comment