Explain multiple increments in single statement | Sololearn: Learn to code for FREE!

+1

Explain multiple increments in single statement

confusing https://code.sololearn.com/csBzub9iEwo1/?ref=app https://code.sololearn.com/csBzub9iEwo1/?ref=app

6/13/2017 8:59:13 AM

+11

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

+11

@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...

+10

@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! ☺🌹

+4

sure; int a = 10; a++; cout << a; a++; cout << a;

+3

:/ are you confused why it says 1122?

+3

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

+3

try it with putting each into a seperate variable then couting that addition

+3

@Andrea. It tells us that doing it the way that is being tried is wrong, does it not?

+3

@Andrea: well it does. because + has order precidence. Pre and Postfix operators have no precidence when used in the same line of a function

+3

that is how it is done but.. your question is "why does this thing that will never work, not work"

+2

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

+2

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

+2

it is considered two seperate statements

+2

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.

+2

i said swap but not there XD

+2

yes jay it work with separate instruction

+2

like int a = 10; int b = f(a) + f2(a)

+2

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

+2

thanks

+2