Microsoft Interview Question
InternsCountry: United States
Interview Type: Phone Interview
{
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
}
//what about this question if a expression evalutes from right to left ,,,,i am confuse in whether a expression evalutes from left or right please help
o/p will be 4 2 0 1
evaluates from left to right
when i goes to ++ it will be 4..No further action is required in case of OR operator(Because in case of OR only one true statement is required ).
The proirity of + is greater than ++ hence would get exectued first
and + operates from right to left
hence if Expession is a + ++a then a gets 11 on execution of right side of + operator i.e a++ ....
now 11+11=22
b=22
a=11
The answer is correct: it's undefined. It's not about operator precedence, it's about sequence points (SP) which define whether side effects have been been guaranteed to have been performed. Since there's no SP between a++ and a, the standard says the value of the expression is undefined. Look at the Wikipedia page for sequence points. It points to the standards.
here the expression is a=a+++a
so by operator precedence a++ i.e postfix is evaluated first and then add with a
but Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
so answer will be b=20 a=11
compiler will not report an error and their is no ambiguity...the expression b=a+++a will always evaluates to b=(a++)+a as postfix operator++ have higher precedence than prefix operator++
#include <iostream>
using namespace std;
int main()
{
int a=10,b=2;
b=a+++a;
cout<<b<<" "<<a<<"\n";
return 0;
}
out_put
------------------------------
20 11
Press any key to continue . . .
on both TC or DEV ........
b=a+++a;
compilers evaluated as "b=(a++)+a not b=a+(++a)" in both compilers
i am not sure but i think compiler evaluate expression from right to left ,it first take last a then add it to second one a, after addition it increase a's value then it become 11.
according to u(right to left) then why b=20......So will be 21.
there are post increment! (a++) +a
a=11 bcoz value of 'a' read (access) once again after the increment .........So 11
b=a+++a is not undefined :
a+++a is (a++) + a.
as compiler's follow greedy approach to get operator . hence a+++a is a++ + a and not a + ++a
don't we need to consider the sequence point concept here
i think the result will b undefined
correct me if m wrong plz
Because there's no sequence point: en.wikipedia.org/wiki/Sequence_point
Under section "Sequence points in C and C++", read Item 4
If u increment a before using in the expression as..
a++;
b=a+a;
then meaning of the both statements are same. But u'll get the answer as a=11 and b=22
why b is not 21 , because [ post / pre ]increments are performed when they got [ && / || / ; ] symbols ,
like a++&&a , here a will increment .
&
a++ ;
a // here a will increments .
@justneel
we are incrementing a i.e 10+1 and not 20+1 becoz we did not assign 20 to a again
There are 2 ways of drawing the expression tree for this expression: a+++a
They are as follows:
{
.......(+)
..../.........\
(a++)......(a)
}
or
{
.......(+)
..../........\
..(a)...(++a)
}
Hence, b=21 and since a is incremented once, a=11
It is 20 and 11 for the fact that lexer embraces the longest token that matches a valid C token while consuming characters from left to right. So, it will tokenize the string
+++
rendering tokens {{ ++ }} followed by a
+
.
on java I do the following code
int a = 10;
int b = 2;
b = a+++a;
System.out.println(a+","+b);
it printed out 11,21. I think the way java do that is it find first a ++
and use 10 in for now, but increase it immediately afterward (before evaluating the second a), and when adding the second a, it is already 11, which gives b = 21.
When I tried that on GCC, it gives 11, 20. I think it is because it increase the value of a after the entire expression is evaluated, meaning a is increased after the second a is read.
The following code also prove that
in java it gives 13, 33
int a = 10;
int b = 2;
b = a++ + a++ + a++;
System.out.println(a+","+b);
but in C it give 13, 30.
ohhh my god why are u making it so complex....
final answer will be:
a=11(as the postfix expression execute in the next line)
b=20(10 +10)
this code contains some coding error , like header file must be iostream.h and and in line cout<<b<<" "<<a"\n" ,it must be cout<<b<<a<<"\n" whithout any coding error it give answer 20 and 11, it first evaluate expression a+++a then increase a's value.
The order in which a++ and a get evaluated is not defined, and so this will be undefined behavior as different execution orders will lead to different results.
- eugene.yarovoi July 24, 2012