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.

Post History

84%
+14 −1
Q&A What is the difference between operator precedence and order of evaluation?

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)...

2 answers  ·  posted 4y ago by Lundin‭  ·  last activity 1mo ago by Alexis Wilke‭

#4: Post edited by user avatar Lundin‭ · 2021-02-01T11:54:34Z (almost 4 years ago)
Formatting
  • 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()`?
  • 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()`?
#3: Post edited by user avatar Lundin‭ · 2020-09-24T06:41:34Z (over 4 years ago)
#2: Post edited by user avatar Lundin‭ · 2020-09-23T14:17:28Z (over 4 years ago)
  • 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? And when I try the identical code on another compiler, I get the opposite order `c b a 7`.
  • Shouldn't operator precedence guarantee that `b()` is executed before `a()`?
  • 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()`?
#1: Initial revision by user avatar Lundin‭ · 2020-09-23T10:16:37Z (over 4 years ago)
What is the difference between operator precedence and order of evaluation?
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? And when I try the identical code on another compiler, I get the opposite order `c b a 7`.

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