Tuesday, September 18, 2007

Technical question

main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}

5 comments:

  1. 4 5 5 4 5
    printf start from right n continue 2wards left

    ReplyDelete
  2. yup the answer is 4 5 5 4 5
    whenever a function is called the the values are put in the stack from left to right and are poped one after the other in the right to left order hence the evaluation takes place from right to left(it is not anly the printf function).

    ReplyDelete
  3. ok but

    from right to left

    i = 5
    --i = 4
    ++i = 5

    i-- = 5 and due to post increment "i" value will be 4

    i++ = 4 and due to post increment "i" will be 5

    ReplyDelete