2
int main()
{
    int x = 3, z ;
    z = x / + + x ;
    printf ("x = %dz = %d", x , z );
    return 0;
}

I thought the output will be x=4 z=0 or x=4 z=1. But I'm getting x=3 z=1.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
user2713461
  • 387
  • 2
  • 5
  • 16

2 Answers2

3

Try removing spaces between ++ (increment operator). Use ++x or ++ x. Compiler may be interpreting it as +(+x), i.e. unary + operator.

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • 6
    Ironically, that means in its current form the behaviour is perfectly defined. – Medinoc Sep 02 '13 at 08:51
  • @Medinoc : Right, may be here it is, but I think it may happen that on seeing a string of `/ + +` some compiler report Compile Error. – 0xF1 Sep 02 '13 at 08:53
  • @nishant Why would "some compilers report Compile Error"? That's a perfectly valid sequence of operators. –  Sep 02 '13 at 09:09
  • @H2CO3 : I didn't tried running it, but `/ + +` lacks operands between the two `+`. I thought some compiler may interpret it this way and report error. Is it so that it will always be interpreted as `+(+x)`? – 0xF1 Sep 02 '13 at 09:13
  • @nishant It's absolutely unambiguous. One `/` operator then two unary `+`. Any compiler rejecting it is wrong. –  Sep 02 '13 at 09:14
1

remove spaces in between two pluses

z = x / ++ x ;  //will gives z value as 1 always  

//except when x=-1 (Floating point exception )

this Might have Undefined Behaviour because Lack of sequence point.

rather than above if you could try like this.

int x = 3, z=3;
printf ("x = %dz = %d", x , z );

z/=(++x); // z/=++x; is also same.  
printf ("x = %dz = %d", x , z );
Gangadhar
  • 10,248
  • 3
  • 31
  • 50