1 Replies - 523 Views - Last Post: 15 March 2015 - 03:24 AM Rate Topic: -----

#1 VIPERHlr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 05-August 11

Can somebody explain this output

Posted 15 March 2015 - 03:05 AM

#include <stdio.h>
int main( )
{
int a = 3, b = 7, c = 4;
double x = b++ * (b - a - 1) / (--c);
printf("%lf",x);
}


How does this output 9 ?, I thought of it as doing the bracket part first which is (7-3-1)/3 = 1 * 8 = 8
Is This A Good Question/Topic? 0
  • +

Replies To: Can somebody explain this output

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Can somebody explain this output

Posted 15 March 2015 - 03:24 AM

Yes, the code exhibits undefined behaviour - b++ and b are used at the same time.

http://www.dreaminco...ost__p__1042539

Neither parentheses nor operator precedence determine when pre and post increment happen. The standard merely states that such effects happen before the next sequence point (a term you should familiarise yourself with).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1