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;

转载于:https://www.cnblogs.com/robin-he0221/archive/2012/09/15/2685908.html

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐