+ 1
Prefix and Postfix Increment and Decrement of C++, Java, and C#
Why is the output of C++ -1, while Java and C# will output 0? #C++ #include <iostream> using namespace std; int main() { int x = 6; x = (++x) - (x--); cout << x << endl; return 0; } #Java public class Program { public static void main(String[] args) { int x = 6; x = (++x) - (x--); System.out.println(x); } } #C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int x = 6; x = (++x) - (x--); Console.WriteLine(x); } } }
2 ответов
+ 6
It's to do with sequencing / sequence points.
https://en.m.wikipedia.org/wiki/Sequence_point
+ 3
https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior