cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
simple ques operator precedence
simple ques operator precedence
Apr 9, 2016 at 8:00pm UTC
technologist
(607)
Hi
I was reviewing precedence order way back and I couldn't remember/understand why the following expression yielded the result it has:
1
2
3
x = 5 + (7 % 2);
//x = 6
why 6?
thx
Last edited on
Apr 9, 2016 at 8:00pm UTC
Apr 9, 2016 at 8:05pm UTC
Moschops
(7244)
7%2 gives 1.
5+1 gives 6.
Apr 9, 2016 at 8:08pm UTC
ChajusSaib
(96)
(7 % 2);
// yields 1
therefore:
x = 5 + 1;
//x = 6
C++ Operator Precedence:
http://en.cppreference.com/w/cpp/language/operator_precedence
Modulus Operator:
http://stackoverflow.com/questions/12556946/how-does-the-modulus-operator-work
Last edited on
Apr 9, 2016 at 8:13pm UTC
Apr 9, 2016 at 8:25pm UTC
technologist
(607)
Thx, I've got the operator precedence down. The trick was reviewing the modulus operator: it required integer division that evaluated to an integer result (+ remainder). I understand now.
Thnx!
Last edited on
Apr 9, 2016 at 8:28pm UTC
Topic archived. No new replies allowed.