int f(int a, int b int c){
return a * b / c;
}
If so, what is f(5,1,3) ?
> Is the following function well defined?
In general, yes, but not if overflow occurs (undefined) or c is 0
(undefined) or if either operand is negative
(implementation-defined).
>
> int f(int a, int b int c){
> return a * b / c;
> }
>
> If so, what is f(5,1,3) ?
5 * 1 / 3 is equivalent to (5 * 1) / 3, which is equal to 5/3 = 1,
because multiplication and division (which have the same precedence)
associate left-to-right.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
> 5 * 1 / 3 is equivalent to (5 * 1) / 3, which is equal to 5/3 = 1,
> because multiplication and division (which have the same precedence)
> associate left-to-right.
Thank you. Every compiler I used said that, but I couldn't find the
"associate left-to-right" bit in t'standard.
That's because associativity isn't spelled out explicitly in the
Standard; it's implicit in the grammar, and is given only the
briefest of passing mentions in the text.
No, it's a syntax error.
> If so, what is f(5,1,3) ?
A function call. To a function for which no valid
definition has been exhibited.
--
Eric Sosman
eso...@ieee-dot-org.invalid
Since I'm in a nit-picky mood...
The last is true only in C89/90, and only if the result is not exactly
an integer. C99 specifies the behavior.