47 AnswersNew Answer
Which part confuses you? int a=10; cout<<a++ + ++a<<endl; //10 + 12; the first a is 10, but because of postincrement and consequential preincrement we get 12 for the second a a=10; cout<<a++<<++a<<endl; // it first increments second a to 11 without printing anything, then it starts from the beginning : it prints first a which is 11 now, and then increases it for 1 (because of the postincrement operator) and prints second a which is 12 now cout<<a; Got it? P. S. You can read more about undefined behaviour in these links: http://c-faq.com/expr/evalorder2.html https://stackoverflow.com/questions/33445796/increment-and-decrement-with-cout-in-c
@Krupesh I think that is the undefined behaviour of compilers that is explained in those two links in my previous post. I don't have any other explanation... It must be it. Edit: I mean, it's kind of logical actually, when you think about it... In a regular code that fixes some problem you would never really write ++a + a++, right? Why would anybody use that, except when practicing operators? So, I understand if compilers see it as some sort of a mistake...
@Krupesh Yes, but we should definitely know about them and how they work, and what they do...etc. Even about this undefined behaviour. Honestly, in spite of a little frustration and a few loses, these challenges were quite helpful in certain areas. You know what they say: practice makes perfect. 👊 Happy coding! ☺🌹
sure; int a = 10; a++; cout << a; a++; cout << a;
:/ are you confused why it says 1122?
Basically the compiler (the thing that interprets the code) does not have any order to which to do the addition. Does it do the prefix or postfix first? see here: https://stackoverflow.com/questions/23368530/undefined-behavior-of-postfix-or-prefix-increment-in-function-calls-in-c
try it with putting each into a seperate variable then couting that addition
@Andrea. It tells us that doing it the way that is being tried is wrong, does it not?
@Andrea: well it does. because + has order precidence. Pre and Postfix operators have no precidence when used in the same line of a function
that is how it is done but.. your question is "why does this thing that will never work, not work"
sum 22 is easier a start from 10 a++ RETURN 10 & increment a so a = 11 ++a increment a so a = 12 then RETURN it So you do 10 + 12
Well there are two << in the line which is the same as typing cout << 11; cout << 12; (obviously without the pre/postfix operators) as there are no spaces inbetween the cout/s what is viewed on the screen is 1112 (11 then 12
it is considered two seperate statements
mmhmmm you are right in how it works. The example you have is actually what is called undefined behaviour. Multiple pre/post operators in a single cout statment confuse the compiler. On some computers it will be 1012 others it will be 1112.
i said swap but not there XD
yes jay it work with separate instruction
like int a = 10; int b = f(a) + f2(a)
is not defined but i cant find the right order of instruction that the compiler create for have 1112 in outpuy. Can u help me @jay? onlu 4 fun XD