. x= ++a + ++b; What is the value of y. if a-5,b=5. *
Answers
Explanation :
The value of a = 5 and a++ is post increment so the answer of a++ will be 5 ,then
b=5+5=10
Hope you liked it and don't hesitate to upvote this answer
Why?
Why do you ask this question?
Are you just trying to pull everyone’s leg?
What do YOU think will be the answer?
Languages do have some ambiguous constructs and such statements should never be used - if you are serious about programming and are trying to solve a genuine problem.
That would be horribly irresponsible programming.
What will happen? Some compilers may give one answer, others give another answer, yet others may refuse to compile. Try it out for yourself on different systems.
Since this is about C, read Kernighan and Ritchie book a bit more carefully and see what they say about cryptic
In C++ post increment operator follows the rule USE-THEN-CHANGE
So the value of b will be calculated first and then value of a will be incremented.
So, b=a+a++
b=5+5
b=10
a increments to a+1 i.e a=6.
The order is first pre increment is done, assign the value, then perform the operation , after the operation give the result, then in last perform the post increment.
In given expression, there is no pre increment, assign value of a as 5, then on performing operation we get result 10, then in last post increment will be done .Hence, value of b will be 10 & a will be 6.
b = a + a++;
b = a + 5;
b = 6 + 5;
b = 11;
since, priority of ++ is greater than +, a++ is evaluated first, then a’s value is incremented by 1, then binary addition is performed between a’s new value and old value.
a++ ←this occurs later.
++a ←this occurs now.
a = 5
b = a + a++
‘b’ will equal 10. After that, ‘a’ will then equal 6.
Answer will be 10 since in the case of assignment of value to any variable, execution of code segment is done from leftto right. There is a post increment sign so the value of a increases after this line gets executed.
it is actually compiler dependent. these are results from turbo c++
it is:
b = 10
a = 6
The value of b is 10.
This is because of a++ which is a post increment.
So the value of a is incremented on the next statement not in the statement b=a+a++.
A=5
A=A+(A++)
A=5+1.
A=6
6+6=12
a = 5
b = a + a++ = 5+(5+1) = 11
answer: b = 10
a = 6
11
10
please mark me as brainlieast and thanks for my answer please