2 Replies - 333 Views - Last Post: 11 January 2013 - 12:08 AM Rate Topic: -----

#1 rk_p  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-January 13

printf() and Increament Operator in C

Posted 10 January 2013 - 11:03 PM

Hello,
I have following code snippet

#include <stdio.h>
int main(int argc, char *argv[])
{
      int i=5;
        printf("%d %d %d %d %d\n",i++,i--,++i,--i,i);

      return 0;
}


When i calculated on my own i have answer as 4 5 5 4 5
and when i checked on compiler it gives different answer as 4 5 5 5 5

I know that in printf,its upto compiler that whether it evaluates using left to right or right to left,
but still it shows different behavior.

Is This A Good Question/Topic? 0
  • +

Replies To: printf() and Increament Operator in C

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: printf() and Increament Operator in C

Posted 10 January 2013 - 11:15 PM

Take a look at This excellent post and you will certainly find your answer.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: printf() and Increament Operator in C

Posted 11 January 2013 - 12:08 AM

This is undefined behavior, take a look at Sequence Points
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1