I am using Dev-C++ compiler. This program is supposed to print 30
but its printing 384
.
#include <stdio.h>
int main() {
int n = 3;
int ans;
ans = n<<3 + n<<1;
printf("%d", ans);
getch();
return 0;
}
The problem is that the +
operator has higher precedence than the <<
operator. What you wrote actually means:
ans = n << (3 + n) << 1;
What you actually want is:
ans = (n<<3) + (n<<1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With