Understanding the Behavior of i + ++i in D Language

Me'vâ mevailm at outlook.com
Fri Aug 23 08:58:16 UTC 2024


Hello,

I'm working with a simple piece of code in the D language and 
encountered a result that has left me puzzled. The code snippet 
is as follows:

```
import std.stdio:writeln;

void main() {
     int i = 5;
     writeln("Result: ", i + ++i);
}
```

When I run this, it surprisingly outputs 11. I tried something 
similar in C before and it gave me 12. I’m curious, why is there 
a difference? How is i + ++i evaluated in D that it ends up 
giving 11 instead of 12?

Is there something about operator precedence or evaluation order 
in D that I'm missing? I'd really appreciate it if someone could 
break it down for me or point me towards some resources to get a 
better understanding of what's going on.

Thanks a bunch!


More information about the Digitalmars-d-learn mailing list