Saturday, August 20, 2011

a tricky printf again...........

Here is a question which i am going to explore best to my concepts and knowledge,you can judge for yourself whether i am right or wrong..
int main()
int val=10;

Before evaluating this ques let us first evaluate the following printf statement

output for this printf:
Explanation::As we know that printf first take the format string and according to the format specifiers in the format string it prints the arguments supplied to that..In this statement:
Format string is :%d
and the variable list id:val+a,"=%d",val+2
So this prints the val+1 that is 11 for the %d and leaves the another variables....

Now what if we want to print the remaining variables.......that so simple as we can see that the 2nd variable is a string and the third argument is an integer in the variable list so we should have the format string as given below:
format string----->:%d%s%d
and variables list is----->val+1, "-%d",val+2

so the output for this will be------>:11-%d12 
that is -%d is the string in this variable list so we have to use the %s format specifier to print this....

Now coming back to the real question,in the real question the printf statement is :
If strictly speaking the result of this is undefined as you can read the sequence point post ,
No doubt the evaluation will be from right to left but we can't assure the result...but let us suppose everything goes according to our will or thinking that is smooth evaluation form right or left then first val-- will be evaluated that will decrement the val to 9 then val+1 which lead to print 10 as the output
so the output will be----->:10
To prove the evaluation is  from right to left let us print the value of val--;

You might not be satisfied with the prove of right to left evaluation so just change the val-- to --val because above example shows the postfix property that is printing before decrementing

Now i thing it will be cleared to you....

 May be i am wrong,so please do comment whether you agree with me or not????
if not give the explanation..... 

No comments:

Post a Comment

Feel free to comment......