Iam not an expert, but i am somewhat familar with incrementation and decrementing variables.
also, i've done my research from some documentation and stackexchange answers. there is a problem with the question and what it uses.
in the line
a = ++a + ++a;
it is altering the same variable more than once in a single expression.
in c, this is considered undefined behavior. different compilers will evaluate this line differently. you cannot get a definite answer. you will not see code like that in a real-life program. you are discouraged from writing code like that.
google up terms such as "increment decrement operator multiple" if you want to read more about this. you should probably bring that "a=++a + ++a; " line up with your teacher after doing some research of your own.
i will present some sort of assumption in my explanation when we come across that line. the explanations afterward involving different variables in a statement using ++ and -- should be definite and logical.
consider the line of code
{
int a = 10
int msg = 5 + a++;
print msg, a;
}
the output will be 15 and 11 (in other words, msg is 15 and a is 11)
a++ will increment after it is used in a statement.
the value used for "a++" in the line of cone is 10, not incremented. then after that line of code, the value of a will be increased by 1.
consider the line of code
{
int a = 10
int msg = 5 + ++a;
print msg, a;
}
the output will be 16 and 11 (in other words, msg is 16 and a is 11)
++a will increment before it is used in a statement.
the value of a gets increased by 1 before it is used in the statement. so the actual math here is 5 + 11.
also, seeing -- has the same behavior, except it will decrement a variable by 1.
so --variable decrements the variable before it is used and
variable-- decrements the variable after it is used.
let us go through the example code step-by-step.
int a=10,b=5;
a = ++a + ++a;
i've mentioned at the top that the second line is an undefined statement, where different c compilers will yield different results.
i guess in this case
a = ++a + ++a;
will have the final value of a be 24. my guess is that it will increment the variable a twice before the statement is even run; what actually gets added up is (10+2) + (10+2) = 24. again, it is bad code.
this next batch of code is more workable; it is not incrementing/decrementing a variable multiple times in one line.
before the code below, we have a=24 and b=5.
b = a-- - --b;
the a-- means that a will be decremented after it is used in the statement. so what is actually used for a-- in the statement is 24, not 23. it is only after that line of code when a becomes 23.
the --b means that b will be decremented before it is used in the statement. so what is actually used for --b in the statement is 4. it decremented the initial value of 5 before it used it.
so the actual math of that line becomes b = 24 - 4, which is 20.
after that line, the value of a at 24 is decremented by 1 to become 23.
thus making the output 23 and 20.
i hope this you.
and you really should bring that problematic line up with your teacher.