Fun surprising things

Patrick Schluter Patrick.Schluter at bbox.fr
Wed Oct 31 15:00:52 UTC 2018


On Wednesday, 31 October 2018 at 14:54:48 UTC, Patrick Schluter 
wrote:
> On Wednesday, 31 October 2018 at 14:00:14 UTC, bauss wrote:
>> What's the output of this program? (Try to figure it out 
>> without running it.)
>>
>> ```
>> int c = 0;
>>
>> int a()
>> {
>>     return c++;
>> }
>>
>> int b()
>> {
>>     return c--;
>> }
>>
>> void main()
>> {
>>     import std.conv : to;
>>     import std.stdio : writeln;
>>
>>     writeln(to!string(a) ~ to!string(b));
>> }
>> ```
>>
>> If your answer is "10" then you're wrong.
>>
>> You can get the result and answer here: 
>> https://run.dlang.io/is/idZurR
>
> 01 is the other possible result. It all depends in what order a 
> and b are called. ~ is not a sequence point (does D even have 
> that typical C notion of sequence points?) so the order of 
> evaluation is at the discretion of the compiler.
>
> therefore -10 or 01 are both right.

And I just checked, ldc gives 01 as response.


More information about the Digitalmars-d mailing list