This is a really simple question, but I'm afraid it's a bit tricky.
Which of the following symbols has the highest precedence? (C++)
a. %
b. -
c. ||
d. &&
I know it sounds easy, but I'm really wondering if whether the answer is % or -. The symbol - can be seen as either a unary minus (which is higher than %) or subtraction (which is lower than %).
What do you think?
A google search for c++ Operator Precedence yielded this:
http://en.cppreference.com/w/cpp/language/operator_precedence
According to the link, Multiplication, division, and remainder come before Addition and subtraction and further down the list comes the Bitwise operations found in row 5,6,13, and 14 respectively.
I would assume the person who made this question wanted you to choose the operator that has the highest possible precedence.
I would choose b.
Related
I either want to set them to bool values or simply integer values so that I can tell my function to multiply/divide these two integers before I add/subtract them to another operand.
Here is my code:
while (!S.empty() && **PRECEDENCE**next <= **PRECEDENCE**S.top())
{
temp = S.top();
S.pop();
postfix.append(temp);
}
Where S is for the stack. So let's say next is the * token and S.top() is '+', so * takes priority over +, so I need to assign a value to * and + so that when they are compared to one another, their values are compared. So the value of * is 1 whereas the value of + is 0.
OMG, there are many methods to do this.
Look up table
You could create a table of records and search the table for the operator, then retrieve the precedence value.
Use switch statement
I don't advise, but it is similar functionality of mapping a precedence to an operator.
Use std::map
Same concept as the other two, except using std::map<char, int> for operator character and precedence.
Hard code the precedence
The operators you compare for first will have the highest precedence. The next operators checked will have lower precedence.
Search the Web and StackOverflow
Hint: Other people have done this assignment. Search StackOverflow or the web for:
"c++ postfix operator precedence"
"c++ calculator operator precedence"
"c++ calculator operator evaluation"
"c++ postfix operator evaluation"
Or use other synonyms that I haven't listed.
Research before posting
Be as smart person and cheat by researching and seeing what other people have done. Before posting questions here.
Use a debugger
Use a debugger or print statements to see where your issues lie.
If you are still stumped, post the issue, your minimal code and the expected behavior.
This came up in a conversation with a friend of my nephew who is home from uni. Personally beside the most frequently used ones I never found it all that important and just use parenthesis. But I don't have to take his exam. Anything clever out there?
Edit Well, a rare moment of near unanimity on SO! If only professors listened.
I know this probably doesn't help you in the context of your exam, but I'll answer the question for anyone else who might stumble upon this:
Don't try to memorize operator precedence, beyond the "common cases" e.g. arithmetic. If your statement is unclear, either split it into multiple statements or toss in parenthesis.
Given that there are 16 levels of precedence, I don't think there's an easy trick to memorizing them (no "Roy G Biv" or other mnemonic that I know of).
IMO, the important ones to remember are postfix > unary (i.e., *p++ == *(p++)), unary > arithmetic (~a+b == (~a)+b), bitwise > logical (a|b&&c == (a|b)&&c)), and that conditional, assignment, and comma operators make up the bottom 3 in that order (a=b,c == (a=b),c).
This is why reference manuals were invented. That doesn't help during an exam, of course.
What I do to remember operator precedence (other than arithmetic, which works the same as arithmetic on paper) is that the unary operator always has precedence. Beyond that, I look it up and use parenthesis, as Steven suggests.
Studying the standard there was no information on how this will expand.
I tried it in visual studio 2008 and it does a = a * (b+c);
Does the standard guarantee that it will always expand to that and not a = a * b + c?
Was this always the way that expression expanded for all c++ standard versions?
Thanks.
Yes it's guaranteed.
a::operator*=(b + c);
EDIT:
Precedence isn't listed in a nice table in the standard, there's a footnote to 5/4 saying:
The precedence of operators is not
directly specified, but it can be
derived from the syntax.
The C++ Reference table is correct though.
Operators like =, *=, etc. have (almost) the lowest precedence (see this reference).
This means that whatever expressions you have on the right side of assignment operator, those expressions will be evaluated first. Then the result of evaluation will be assigned to the variable on the left side of assignment operator (multiplying along the way, in the case of *=).
a *= b + c does not "expand" to anything, *= is not a preprocessor macro.
Yes, modulo some obscure compiler bug the right side has always been evaluated as a unit.
*= is its own operator. It should amount to the same postconditions as writing a = a * (b + c) if the latter is valid but is not guaranteed to use the same path of execution to get there.
The + will have a higher precedence than the *= so it is guaranteed that b+c will be evaluated first, even if a is of a class type and *= is an overload.
*= is a separate operator. For all the built in functions it does the same thing as multiplication and assignment, and if you ever define it for one of your own classes you really SHOULD make it do the same thing, for obvious reasons. However, you wouldn't HAVE to.
That is, *= is its own operator, with its own precedence, and internally it will be represented as "multiplication, then assignment", but at no time is the line ever re-parsed with multiplication and assignment operators in.
Given that, there's two ways that line could possibly be parsed:
As a *= (b+c) (should have the same result as a = a * (b+c) for any sane types)
As (a*=b) + c (which assuming this is a stand-alone statement, will do multiply a by b, assign the result to a. It will then multiply the new value by c, but it will throw the result away)
So you can see, there's no precedence which would make it do "a = (a*b)+c" which is what you feared.
In fact, the second is clearly not very useful, and for this reason, all the assignment and something-assignment operators have lower precedence than arithmetic and other "normal" operations, in fact, lower precedence than anything except ",".
It's easy to check the precedence by googling and finding a table like:
http://www.cppreference.com/wiki/language/operator_precedence
I don't remember all of the details, so it's fine to check if you're not sure between similar operators, but you can do most of it by remembering the general principles like this one.
after this question, I don't know what to think.
In OCaml, if you do something like -1.0**2.0 (because of the typing you need to have float), you obtain 1.00. According to the standard order of operations, the result should be -1 (as in python).
I wasn't able to find the reason or a clear definition of the operator precedence in OCaml...
Is this because of the type system ? or the fact that there's a binding underneath with pow ?
As the very page you quote says, "The order in which the unary operator − (usually read "minus") acts is often problematical." -- it quotes Excel and bc as having the same priority for it as O'CAML, but also says "In written or printed mathematics" it works as in Python. So, essentially, there's no universal consensus on this specific issue.
Operator precedence is syntax-directed in OCaml, which means that the first character of the function identifier (and whether it's unary or binary) determines the operator precedence according to a fixed sequence. Contrast this with languages like Haskell, where the operator precedence can be specified at function definition regardless of which characters are used to form the function identifier.
Given the following:
&row->count
Would &(row->count) be evaluated or (&row)->count be evaluated in C++?
EDIT: Here's a great link for C++ precedence.
As far as precedence rules go, I've always liked the one put forth by Steve Oualline in "Practical C":
There are fifteen precedence rules in
C (&& comes before || comes before
?:). The practical programmer reduces
these to two:
1) Multiplication and division come
before addition and subtraction.
2) Put parentheses around everything
else.
&(row->count)
This is already asked. But here is a link.
Edit:
Ok this question is very similar. And possibly there is an other one.
May I suggest that you resolve such questions using a test programme? That has the advantage that you will know for sure that the answer is correct for your implementation, and you are not exposed to the risk of badly answered questions.
C operator precendence is explained here
As per the table, -> is higher priority than the & operator, so it's &(row->count)
&(row->count)
-> has a higher priority than & (address of). So your expression would be evalutated as &(row->count)