int i=0, j=1;
j %= (i++ || i++==j);
printf("El valor de i es %d\n", i);
printf("El valor de j es %d\n", j);
what's the meaning of ´j%´?
and what happend with this (i++ || i++==j)
int i=0, j=1;
j %= (i++ || i++==j);
printf("El valor de i es %d\n", i);
printf("El valor de j es %d\n", j);
what's the meaning of ´j%´?
and what happend with this (i++ || i++==j)
%= is known as an assignment operator, where
j += 1
is shorthand for
j = j + 1
> and what happend with this (i++ || i++==j)
It's a boolean expression which will have the value 0 (if false), or 1 (if true).
Knowledge of short-circuit evaluation rules will be important.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Here is the result I got:
Which makes sense if you consider:Code:El valor de i es 2 El valor de j es 0
Order of evaluation - cppreference.com
6) Every value computation and side effect of the first (left) argument of the built-in logical AND operator && and the built-in logical OR operator || is sequenced before every value computation and side effect of the second (right) argument.
With the two instances of i++ in the same line, is this not also an example of undefined behavior?
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
Evaluation is specified for ++i and i++ and before or after evaluation. Evaluation in this case means specifically the value of the variable, not the outcome of the function, so it shodul always have the effect of e.g.
x = 5 then
5 || 6
with x being 7 after the fact.
I wouldn't write such a piece of code though, as this may be an area where some compilers are non-compliant (ahem MSVC).
No, because || introduces a sequence point.Originally Posted by Elkvis
Not quite: the issue is that between consecutive sequence points, if i is modified more than once, then the behaviour is undefined. Thankfully, || introduces a sequence point, so there is no issue. If it were | instead, then the behaviour would be undefined.Originally Posted by abachler
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
abachler! Haven't seen you on here in over 4 years! Where have you been? What have you been up to?
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?