Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
int main() {
int n = 2;
int a = 1 << n + 1;
int b = n > 1 ? 10 : 20 + 1;
std::cout << a << ' ' << b << '\n';
}8 10
- Expecting
5fora:+has higher precedence than<<, so1 << n + 1is1 << 3, which is8. - Expecting
11forb:?:has lower precedence than+, so20 + 1is the whole third operand, andbgets10.
- Where would you add parentheses so that
abecomes5andbbecomes11? - Why does
std::cout << n > 1;fail to compile whilestd::cout << (n > 1);works?