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()
?
1 comment thread