What is the output of the java expression a= 3,b=5, c=10
++C+b+b+ c+a++
Answers
Answered by
39
Considering the way you have written your expression, there are 2 expressions I can infer from it:
- c = (a++) + b
- c = a + (++b)
In our expression, we have a post increment operator and an addition operator, as well as a pre increment operator and an addition operator.
- Pre increment operator: This operator works on the principle of 'change and use'. So we first change its value and then substitute the new value in our expression.
Ex. ++a + b + a = 3 + 5 + 3 = 11
- Post increment operator: This operator works on the principle of 'use and change'. So we first use it and then increment its value in memory for later use.
Ex. a++ + b + a = 2 + 5 + 3 = 10
In C, there are two concepts, precedence and associativity.
- Precedence: The predefined rule of priority of operators is called operator precedence. Rules are, pre increment and post increment operators have greater priority than the addition operator.
- Associativity: The order in which 2 operators of same precedence are executed is called associativity. Rules are, post increment and pre increment operators have right to left associativity whereas addition operator has left to right associativity.
So now trying to solve your question, here associativity does not really come into picture because there are no multiple operators of a similar kind, though we will solve our expression from left to right.
- c = (a++) + b = 2 + 5 = 7
- c = a + (++b) = 2 + 6 = 8
Similar questions
India Languages,
2 months ago
English,
4 months ago
Math,
4 months ago
Math,
10 months ago
Computer Science,
10 months ago
Science,
10 months ago