-4

I have a doubt in the following code snippet

float func(float x, float y)
{ 
 return x*0.5 + y;
}
int main()
{
 float x = 0.5;
 if (x = func(x, x++))
 {
  printf("%f\n", x);
 }   
}

The answer of the above code comes to 1.250000. In the function call x++ means 0.5++. Function passes 1.0 through x++ parameter. I want to know what does 0.5++ mean? why it adds one more 0.5 instead of adding 1.0? Please help me

  • 11
    `x = func(x, x++)` is undefined behavior. – Yu Hao Aug 26 '15 at 10:56
  • 5
    `0.5++` would be an error; `++` may only be applied to lvalues – M.M Aug 26 '15 at 11:02
  • http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior – M.M Aug 26 '15 at 11:05
  • Aside from all the undefined behavior, did you mean `==` instead of `=`? – Jens Aug 26 '15 at 11:08
  • 2
    Plase STOP writing code that is ambiguous at best, UB at worst. Would like to debug that crap? No? Then don't write it, and don't post it here for explanations:( – Martin James Aug 26 '15 at 11:08
  • I'm voting to close this question as off-topic because seeks explanation of bad code and is of negative value to future SO visitors. – Martin James Aug 26 '15 at 11:09
  • 2
    @MartinJames please do not do that. I m seeking explanation for one grave doubt i had. can u please tell me what is 0.5++? Is it 1.0 or 1.5 and also why/ – Shanbhag Vinit Aug 26 '15 at 11:12
  • @Jens it is assignment using =. The value returned by the function has been assigned back to x – Shanbhag Vinit Aug 26 '15 at 11:19
  • 1
    Why are you seeking explanations for bad code? Why do you think it's useful for future visitors to SO? Why is the doubt so grave? 'func(x, x++)' is just stupid anyone who looks at it can see that. Why are you so insistent on writing really, obviously, bad code and then asking others for explanations? It's irrational behaviour, as well as undefined. – Martin James Aug 27 '15 at 09:25
  • Now, come clean - you didn't really write this rubbish yourself, did you? – Martin James Aug 27 '15 at 09:27

4 Answers4

7
(x = func(x, x++))

This exhibhit's undefined behaviour. As you tend to change value of x more than one time in a single execution line.

As per standards

C99 §6.5: “2. Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.”

Apart from this-

As you asked in comment what will be x++;?

As ++ increment value by 1 ,thus expression x++; will yield you 1.5.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • I hope not. It executed properly to yield answer 1.25 – Shanbhag Vinit Aug 26 '15 at 11:01
  • 2
    @user282528 hope doesn't get you far in C++! :) – M.M Aug 26 '15 at 11:02
  • 2
    @user282528 Undefined bbehaviour can give you anything .Thus it is possible that it can give you correct answer. – ameyCU Aug 26 '15 at 11:02
  • @ameyCU leave all that execution sequence, can u tell me what is 0.5++? Is it 1 or 0.5+1=1.5? Also how? – Shanbhag Vinit Aug 26 '15 at 11:08
  • @user282528 ++ means add 1 of the appropriate type. So if you fix your code to remove the undefined behavior and change the code so that it actually performs `float x=0.5; x++`, you should get the answer 1.5. – Lundin Aug 26 '15 at 11:10
  • @user282528 `0.5++` is `1.5`. To verify print the value of `x` and `y` in `func()`. Either `x` or `y` will be `1.5` – Santosh A Aug 26 '15 at 11:11
  • 1
    @user282528 That would lead to 1.5. Try and print it .See here fr example-https://ideone.com/oBErHD – ameyCU Aug 26 '15 at 11:11
  • @user282528 you need to know what [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) is. http://catb.org/jargon/html/N/nasal-demons.html – phuclv Aug 26 '15 at 11:29
7

Point 6.5:2 of the C99 standard says:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

There is no sequence point between the evaluations of function arguments, so func(x, x++) breaks this rule and hence its behaviour is undefined.

Regarding 0.5++, that expression is illegal since it is equivalent to 0.5 += 1, and you thankfully can't assign to constants. However, the following code:

float x = 0.5f;
x++;
printf("%f", x);

is equivalent to

float x = 0.5f;
x += 1;
printf("%f", x);

and will print 1.500000 as expected.

nemetroid
  • 2,100
  • 13
  • 20
2

As others have explained, the posted code contains undefined behavior and, as such, is meaningless.

So let's assume you changed your question to...

Can you please tell me what is 0.5++? Is it 1.0 or 1.5 and also why?

Neither: 0.5++ is not valid C code.

C99 §6.5.2.4 Constraints: 1. The operand of the postfix increment [...] shall be a modifiable lvalue.

This means it must be something where a value can be stored. 0.5 is a constant and can't be modified.

If you again change your question to:

float x = 0.5;

printf("%f\n", x++);

What's the printed value?

... then we're finally down to proper C. The value printed would be "0.500000", as the result of the postfix operator is the value before the increment.

The new value of x is 1.5. See

C99 §6.5.2.4 Semantics: 2. The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
-5

The line:

if (x = func(x, x++))

should be:

if (x == func(x, x++))
Vito Royeca
  • 657
  • 10
  • 20