+ 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); } } }

22nd Apr 2019, 1:24 AM
Jerald Cris Bergantinos
Jerald Cris Bergantinos - avatar
2 Réponses
+ 6
It's to do with sequencing / sequence points. https://en.m.wikipedia.org/wiki/Sequence_point
22nd Apr 2019, 8:12 AM
Sonic
Sonic - avatar
+ 3
https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior
22nd Apr 2019, 5:13 AM
Anna
Anna - avatar