Interview questions 1 : What does the following program print?
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int x = 2, y, z; 8 9 x *= (y = z = 5); cout << x << endl; 10 11 z = 3; 12 x == (y = z); cout << x << endl; 13 14 x = (y == z); cout << x << endl; 15 16 x = (y & z); cout << x << endl; 17 18 x = (y && z); cout << x << endl; 19 20 y = 4; 21 22 x = (y | z); cout << x << endl; 23 24 x = (y || z); cout << x << endl; 25 26 return 0; 27 }
1. x *= (y = z = 5); cout << x << endl; 相当于x *= y; x = 10;
2. z = 3; x == (y = z); cout << x << endl; x的值未改变 x = 10;
3. x = (y == z); cout << x << endl; (y == z)值为1, x = 1;
4. x = (y & z); cout << x << endl; (y & z)值为3, x = 3;
5. x = (y && z); cout << x << endl; (y && z)值为1, x = 1;
6. y = 4; x = (y | z); cout << x << endl;(y | z)值为7, x = 7;
7. x = (y || z); cout << x << endl; (y || z)值为1, x = 1;
所有评论(0)