Integer ops, associativity and precedence...

G

gwowen

Is the following function well defined?

int f(int a, int b int c){
return a * b / c;
}

If so, what is f(5,1,3) ?
 
G

gwowen

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

robertwessel2

In


In general, yes, but not if overflow occurs (undefined) or c is 0
(undefined) or if either operand is negative
(implementation-defined).


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top