13th Jun 2017, 8:59 AM
Krupesh Anadkat
Krupesh Anadkat - avatar
47 Respuestas
+ 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
13th Jun 2017, 11:20 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 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...
13th Jun 2017, 2:43 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 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! ☺🌹
13th Jun 2017, 6:32 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 4
sure; int a = 10; a++; cout << a; a++; cout << a;
13th Jun 2017, 11:06 AM
jay
jay - avatar
+ 3
:/ are you confused why it says 1122?
13th Jun 2017, 10:33 AM
jay
jay - avatar
+ 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
13th Jun 2017, 10:54 AM
jay
jay - avatar
+ 3
try it with putting each into a seperate variable then couting that addition
13th Jun 2017, 10:57 AM
jay
jay - avatar
+ 3
@Andrea. It tells us that doing it the way that is being tried is wrong, does it not?
13th Jun 2017, 11:01 AM
jay
jay - avatar
+ 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
13th Jun 2017, 11:05 AM
jay
jay - avatar
+ 3
that is how it is done but.. your question is "why does this thing that will never work, not work"
13th Jun 2017, 11:08 AM
jay
jay - avatar
+ 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
13th Jun 2017, 10:35 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 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
13th Jun 2017, 10:37 AM
jay
jay - avatar
+ 2
it is considered two seperate statements
13th Jun 2017, 10:39 AM
jay
jay - avatar
+ 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.
13th Jun 2017, 10:48 AM
jay
jay - avatar
+ 2
i said swap but not there XD
13th Jun 2017, 10:57 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
yes jay it work with separate instruction
13th Jun 2017, 10:58 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
like int a = 10; int b = f(a) + f2(a)
13th Jun 2017, 11:02 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 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
13th Jun 2017, 11:05 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
thanks
13th Jun 2017, 11:07 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
ok
13th Jun 2017, 11:12 AM
Andrea Simone Costa
Andrea Simone Costa - avatar