By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

What are Precedence rules for % and *?

P: 1
What is the value of 2%4*3?
Is it 2%(4*3) = 2%12 = 2
(2%4)* 3 = 2*3 = 6

I checked precedence rules and they say % and * have same precedence and left- to- right evaluation in case of same precedence. So, in that case, 6 must be the answer.
But when I checked calculators online, they returned 2 as answer.
So, please help me with references.
Thank you in advance.....
Jun 7 '16 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 2,398
What do you mean by "checked calculators online"?
Are you sure these online calculators obey the same operator precedence rules as the C compiler?

The practical answer to your question is "I don't bother to know". Intended precedence is more important than actual precedence. You should use parentheses in your code to clarify the intended precedence rather than assuming you (and all future maintainers of the code) will remember the details of how the compiler precedence works.
Jun 7 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.