Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on What is the difference between operator precedence and order of evaluation?

Post

What is the difference between operator precedence and order of evaluation?

+12
−1

When doing something simple such as this

int a=1;
int b=2;
int c=3;
printf("%d\n", a + b * c);

then I was told that operator precedence guarantees that the code is equivalent to
a + (b * c), since * has higher precedence than +. And so the result is guaranteed to be 7 and not 9.

However, when I modify the above example like this:

#include <stdio.h>

int a (void) { printf("%s ",__func__); return 1; }
int b (void) { printf("%s ",__func__); return 2; }
int c (void) { printf("%s ",__func__); return 3; }

int main (void)
{
  printf("%d\n", a() + b() * c());
  return 0;
}

Then I get the output a b c 7. How is this possible?

Shouldn't operator precedence guarantee that b() is executed before a()?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (10 comments)
General comments
Lundin‭ wrote over 3 years ago

...and the reason for down votes is...?

msh210‭ wrote over 3 years ago

I can tell you only why I downvoted; I can't speak for anyone else. (1) You've tagged this with two different language tags. Which language are you asking about? (2) Part of your question is about a difference in how two compilers act -- but you don't say which compilers you're referring to.

Lundin‭ wrote over 3 years ago

No love for self-answered Q&A I guess. So much for getting this site going... If there is any factual errors in the post, I would very much like to know about them so I can improve it.

Lundin‭ wrote over 3 years ago

@‭msh210‭ The C and C++ tags are there because this is a self-answered Q&A post which is meant to be used as a canonical dupe link for C and C++ questions. The languages are identical here. We should not close future C questions with a link to C++ or vice versa. Believe me, I know all about the "C/C++" cross tagging problems that SO suffered from. I was the one who drafted and pushed through the cross tagging policies that SO is now using...

Lundin‭ wrote over 3 years ago · edited over 3 years ago

As for compilers, the very point here is that the code has unspecified behavior and may behave differently between any number of compilers, or even when executed several times on the same one. But fair enough I can remove that part.

msh210‭ wrote over 3 years ago

I've no problem with self-answered questions, but they should still be good-quality questions. This isn't one (for the reasons I mentioned above). No honest asker will (or at least: no honest asker should) ask the question as you did, so you shouldn't either.

Lundin‭ wrote over 3 years ago

@msh210 Fine, I can remove the C++ part since I don't really want to promote that language anyhow. Any remaining problems after edits?

msh210‭ wrote over 3 years ago · edited over 3 years ago

Not IMO, Lundin. Nice edits.

Estela‭ wrote about 3 years ago

I use C++ so I find the information in this Q&A very useful if it applies to C++. Which I think it does even with a verbatim copy. Should I post another question and answer tagged with C++ which is a verbatim copy? Should I ask in meta instead?

Lundin‭ wrote about 3 years ago

@Estela The terms operator precedence, associativity and order of evaluation ("sequenced before"/"sequenced after") are the same in C++. The only difference is that in later versions, C++17 and beyond has added well-defined order of evaluation for certain specific operators such as =.