in Programming in C edited by
888 views
1 vote
1 vote

I have learnt that order of function call of f1() and f2() is compiler dependent from K&R book.Why precedence and associavity does not work in this case ?? Please clarify in details .
 

#include<stdio.h>
int x = 0;
int f1() {
x = 5;
return x;
}
int f2() {
x = 10;
return x;
}
int main() {
int p = f1() + f2();
printf("%d ", x);
return 0;
}
in Programming in C edited by
888 views

4 Comments

Below is statement from K&R book .I dont understand ,pls clarify the meaning .

"x=f()+g();

f may be evaluated before g or vice versa ;if either f or g alters a variable on which the other depends ,x can depends on the order of evaluation .Intermediate results can be stored in temporary variables to ensure a particular sequence."
0
0
yes It is depending on order of evaluation output is 10 because last time f2() modify global variable named x, it you write f2() + f1() output will be 5 based on function calling order. I hope it will clear your doubt.
0
0

I thinks its not compiler dependent......

Because if we write  

int p = f1() + f2();

printf("%d",x);

it output wll be 10.

And if we write

int p = f2() +f1();

its output will be 5.

plz check........

0
0

1 Answer

4 votes
4 votes
Best answer
  • #include<stdio.h>
    int x = 0;
    int f1() {
    x = 5;
    return x;
    }
    int f2() {
    x = 10;
    return x;
    }
    int main() {
    int p = f1() + f2();
    printf("%d ", x);
    return 0;
    }
  • #include<stdio.h>
    int x = 0;
    int f1() {
    x = 5;
    return x;
    }
    int f2() {
    x = 10;
    return x;
    }
    int main() {
    int p = f2() + f1();
    printf("%d ", x);
    return 0;
    }


This is a case of Unspecified Behaviour (Or preferably Compiler Dependent).

Because, Your compiler has the freedom to run the either function $f1$ or $f2$ first . So, your compiler has alternatives to run which one first and it can go with anyone .

Unspecified behaviour comes when your vendor has no idea what compiler will do, hence he/she cannot document that this particular behaviour happens. As, compiler picks any one of the provided alternatives henceforth the name comes Unspecified behaviour .


NOTE :- If you remember race condition from OS

That which thread executes last, will change the output and intermediate threads which run have lost their updates. Same concept is being followed here . Also, note the difference between Unspecified Behaviour and Undefined Behaviour .

selected by
by

4 Comments

@Srestha

Value of $x$ is needed and not value of $p$. $p$ will be $15$ only, but printf asks for the value of $x$, and as it is a global variable.

Hence, if $f1$ executes first and then $f2$, $x$ is set to $10$

Otherwise, $x$ is set to $5$.


Also, Unspecified behaviour comes only when you hve many alternatives of the final output, but cannot guarantee that this behaviour will be shown. Hence, it is different from undefined behaviour .

1
1
yes, that is true.

But what sequence point says?

we cannot do more than one operation on the same variable in between a sequence point.

But is here only one variable?

Here we are getting two different function call, i.e. f1() and f2().

So, why sequence point will be matter here?
0
0
It matters to make something sure like :

$f1()$;

$f2()$;

then print $x$;
0
0

Related questions