-1

From the book which I have read:

Associativity can be of two types:
1-Left to Right
Left to Right associativity means that the left operand must be unambiguous.Unambiguous in what sense? It must not be involved in evaluation of any other sub-expression.
2-Right to left
same as above.

Now please read the following code:

#include <stdio.h>

/* no of item purchased>1000 then discount of 10% else full price */
int main()
{
  int n;
  float r,p,d,t;

  printf("Enter the no of item purchased");
  scanf("%d", &n);

  printf("Enter the price per item");
  scanf("%f", &r);

  if(n > 1000)
  {
    p = n * r;
    printf("Price before discount = %f\n", p);

    d = 10 / 100 * p; //please have a carefull look at d
    printf("discount offered is = %f\n", d);

    t = p - d;
    printf("total price after discount = %f\n", t);
  }
  else
  {
    p = n * r;
    printf("total price is = %f", p);
  }

  return 0;
}

As given in above code "d=10/100*p" it follows L-->R associativity as p is calculated previously involved in evaluation of other expression.
Now changing the d "d=p*10/100" this case correctly calculates the discount but above one gives zero as answer.

Can anyone please explain associativity in context to above example.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 5
    Please read the duplicates before posting them again. And don't tag a C question [tag:c++]. – Lightness Races in Orbit Sep 05 '16 at 20:34
  • I think this may help you: http://en.cppreference.com/w/c/language/operator_precedence – Jesper Juhl Sep 05 '16 at 20:35
  • 1
    since `10` and `100` are integers, the calculation uses integer division: `10/100 == 0`. – tly Sep 05 '16 at 20:36
  • @tly: Please, do not use the comments section for giving the answer. – Lightness Races in Orbit Sep 05 '16 at 20:40
  • 1
    thank you @JesperJuhl for the link,the explanation in the book was not that good :) –  Sep 05 '16 at 20:49
  • @LightnessRacesinOrbit I was not able to find answer related to this from other post, should I delete it? –  Sep 05 '16 at 20:50
  • the posted code contains: `int n; float r,p,d,t;` For ease of readability and understanding, variable names should be meaningful and indicate contents or usage (or better, both) The variables, even in the current context, are meaningless. – user3629249 Sep 06 '16 at 21:30
  • this line: `Now changing the d "d=p*10/100" this case correctly calculates the discount but above one gives zero as answer.` this calculation is using `integer` math. I.E. no remainder. So when using `d=10/100*p` the 10/100 is performed first. since no remainder, the result is 0. and 0 multiplied by any number is still 0. This is one of the `gotchas` of integer math. – user3629249 Sep 06 '16 at 21:35

2 Answers2

3

Both multiplicative * and / are left-to-right operators. They even have the same precedence.

Therefore, your expressions can be re-written like so without changing their meaning:

d = (10 / 100) * p
d = (p * 10) / 100

Since 10 / 100 is an integer division with the result 0, hopefully now you can see the difference.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
-4

C's arithmetic * (and also - and +) operators are associative just as they are mathematics:

These lines evaluate to true:

A * B * C == (A * B) * C == A * (B * C);

(or any permutation of A, B, C, because * is also commutative—i.e., x*y == y*x is true)

However, this in general is not true:

A / B / C == (A / B) / C == A / (B / C);
Geremia
  • 4,745
  • 37
  • 43