+ 3

Why ++i*++i is so weird

30th Aug 2020, 5:18 AM
shubham patil
shubham patil - avatar
6 Respostas
+ 8
Avoid to use such types of calculations . This will give different Output in different plateform. I tried in program i was getting different Outputs in solo and other ide. #include <iostream> using namespace std; int main() { int i=2; int c=++i*++i; cout<<c; return 0; } Some Compilers giving Output as 12 and some giving 16 so its depend on Compilers.
30th Aug 2020, 5:24 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 5
shubham patil It's not always fixed! In java => int x = 3; System.out.println((++x)*(++x)); Output: 20 Because the second increment is happening after the 1st value has already been taken! (++x)*(++x) => (3+1)*(4+1) => 20 But in C, Both the increments are happening in the starting only! So, (++x)*(++x) => (3+2)*(3+2) => 25
30th Aug 2020, 8:12 AM
Namit Jain
Namit Jain - avatar
+ 4
shubham patil Please read this 👇 This is how I came to understand that I should never mix a read and write operation in a single code execution sequence. https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
30th Aug 2020, 6:26 AM
Ipang
+ 1
Yeah ཞıɬıƙą ɱıʂɧཞą i tried on code::blocks an solo Both i increases by 2 at a time and then get multiplied but there is no clear logic
30th Aug 2020, 5:38 AM
shubham patil
shubham patil - avatar
+ 1
https://code.sololearn.com/cgxtDk5Z2fNr/?ref=app Infinity thanks ... but still i get 49 as ans
30th Aug 2020, 5:42 AM
shubham patil
shubham patil - avatar