30

if I have a line of code that goes something like

int s = (double) t/2   

Is it the same as

int s = (double) (t/2)

or

int s = ((double) t)/2

?

Avi Mosseri
  • 1,258
  • 1
  • 18
  • 35

1 Answers1

33

See this table on operator precedence to make things clearer. Simply put, a cast takes precedence over a division operation, so it would give the same output as

int s = ((double)t) / 2;

As knoight pointed out, this is not technically the same operation as it would be without the parentheses, since they have a priority as well. However, for the purposes of this example, it will offer the same result, and is for all intents and purposes equivalent.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Max Roncace
  • 1,277
  • 2
  • 15
  • 40