what will be the value of b?? int a = 2; int b = a++ + a++;
Vivek Sharma
a=2; b=a++ + a++; As we know in an assignment expression assocciativity is right--> left. so here right side a value 2 is taken as the operand and after that a's value 2 increments to 3, and then left side a's value becomes 3. so 3 is taken as another operand and after that 3 is increments to 4. but the addition and assignment performs before a's value becomes 4.
int a = 2; int b = a++ + a++; int c = ++a + a++ + a++; +-----+------+------+----+ | C | C++ | Java | C# | +-----+-----+------+------+----+ | a | 7 | 7 | 7 | 7 | +-----+-----+------+------+----+ | b | 4 | 4 | 5 | 5 | +-----+-----+------+------+----+ | c | 15 | 15 | 16 | 16 | +-----+-----+------+------+----+
Really sorry.... It happened by my poor internet connection....
a= 4 and b = 5
value of a= 4 and b = 5
Depending on the compiler you use... This is a bad programming style
5
b=5
int a = 2; int b = a++ + a++; //right to left value of first a++=2 and then a=3 so second a++=3 after that a=4 b=3+2; b=5;
4
6
int a = 2; int b = a++;int c = a++;int d = b + c;Console.WriteLine("b={0}", b);Console.WriteLine("c={0}", c);Console.WriteLine("d={0}", d);Console.ReadLine();