libin_v
asked on
Order of function call
hi everyone,
Kernighan & Ritchie (in the book the C programming language) says that the order of a function call in an expression cannot be evaluated (section 2.12, third and fourth paragraph)
for eg :
x = f() + g() * h();
the book says any of these functions can be evaluated based on the compiler.
Please explain why is this only for functions,
as everybody knows that when we say
a = 1;
b = 5;
c = 10;
d = a + b * c;
the order of evaluation is b*c (viz = z, say) first and
then a+z (viz = y, say) and then d = y
thus some method of evaluation of expression is used.
such as
1 -> address of a is stored in operand stack (od),
2 -> '+' is pushed into operator stack (otr),
3 -> address of b is pushed into od,
4 -> compares the precedence of the next operator and top
of otr stack, if great pushes '*' into otr stack and
proceeds with next operand
5 -> address of c is pushed into od,
6 -> end of expression, so resolves b and c performes
operation '*' and so on....
thus the order of evaluation is b c and then a.
in the above x = f() + g() * h(),
the function pointers an be stored in the operand stack, thus the order can be dermined.
Kernighan & Ritchie (in the book the C programming language) says that the order of a function call in an expression cannot be evaluated (section 2.12, third and fourth paragraph)
for eg :
x = f() + g() * h();
the book says any of these functions can be evaluated based on the compiler.
Please explain why is this only for functions,
as everybody knows that when we say
a = 1;
b = 5;
c = 10;
d = a + b * c;
the order of evaluation is b*c (viz = z, say) first and
then a+z (viz = y, say) and then d = y
thus some method of evaluation of expression is used.
such as
1 -> address of a is stored in operand stack (od),
2 -> '+' is pushed into operator stack (otr),
3 -> address of b is pushed into od,
4 -> compares the precedence of the next operator and top
of otr stack, if great pushes '*' into otr stack and
proceeds with next operand
5 -> address of c is pushed into od,
6 -> end of expression, so resolves b and c performes
operation '*' and so on....
thus the order of evaluation is b c and then a.
in the above x = f() + g() * h(),
the function pointers an be stored in the operand stack, thus the order can be dermined.
ASKER
hi kdo,
>Replacing a, b, and c with the function calls in your example, >you can see that there are several wasy to build the stack, so >the functions will likely be called in the order that the compiler >intends to place the values on the stack.
>Note also that the Associative Laws of albegra also allow more >combinations than just these two.
Can u please elaborate on the above said statements. (I didnt understand them).
>Replacing a, b, and c with the function calls in your example, >you can see that there are several wasy to build the stack, so >the functions will likely be called in the order that the compiler >intends to place the values on the stack.
>Note also that the Associative Laws of albegra also allow more >combinations than just these two.
Can u please elaborate on the above said statements. (I didnt understand them).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
fine kdo,
i understood this,
but Kernighan & Ritchie (in the book the C programming language) says in section 2.12, third -> "C, like most languages, does not specify the order in which the operands of an operator are evaluated. (the exception are &&, ||, ?: and ','.)".
I didnt understand the exceptions, please explain how the exceptions are evaluted.
i understood this,
but Kernighan & Ritchie (in the book the C programming language) says in section 2.12, third -> "C, like most languages, does not specify the order in which the operands of an operator are evaluated. (the exception are &&, ||, ?: and ','.)".
I didnt understand the exceptions, please explain how the exceptions are evaluted.
ASKER
Kdo,
Do not bother to answer the exception part, I understood it.
Thank U for answering.
Do not bother to answer the exception part, I understood it.
Thank U for answering.
The comment by K&R is a reference to the processing stack.
a+b*c is a+(b*c)
this can be effected on the stack as
abc*+
or
bc*a+
Replacing a, b, and c with the function calls in your example, you can see that there are several wasy to build the stack, so the functions will likely be called in the order that the compiler intends to place the values on the stack.
Note also that the Associative Laws of albegra also allow more combinations than just these two.
Kent