3
Why is this code giving as a result -2 and not 7?
#include <stdio.h>
main()
{
printf("%i\n", 105 % 3*4 - 3*4 % (101 / 10));
system("pause");
}
3
Why is this code giving as a result -2 and not 7?
#include <stdio.h>
main()
{
printf("%i\n", 105 % 3*4 - 3*4 % (101 / 10));
system("pause");
}
6
I did so and gave 7:
#include <stdio.h>
int main() {
printf("%i\n", 105 % (3 * 4) - (3 * 4) % (101 / 10));
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Probably interpreted the expression without considering the precedence and association of operators. The rest operator has the same multiplication and associativity precedence on the left, so in an expression with the two operators the first appearing will be executed. To increase the precedence of an operator you must use the bracketed operator that has high priority.
The second pair of parentheses is not necessary, I put only to help better see the separation. The last pair, which already existed, solves the precedence problem that there could be.
Browser other questions tagged c mathematics operators
You are not signed in. Login or sign up in order to post.