I am practicing C basics for my exam and encountered a tricky situation while doing a simple for loop:
int main()
{
for( double i = 9.999; i > 0; )
{
printf("%d ", ((int)i) /= 2);
}
return 0;
}
I get an error:
error: lvalue required as left operand of assignment
printf("%d ", ((int)i) /= 2);
^~
Isn't (int)i an lvalue? Or how could you rewrite it to get the output: 4 2 1 0?
Isn't
(int)ian lvalue ?
i is, (int)i isn't.
Or how could you rewrite it to get the output:
4 2 1 0?
The best you can do is be clear and separate the statements:
i = (int)i / 2;
printf("%d ", (int)i);
Note that you cannot use i /= 2 in the first one since then you will not get to zero quickly by truncating.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With