associativity - Please explain the output of this simple C program -


this question has answer here:

int = 2, j = 3, k, l ; float a, b ; k = / j * j ; l = j / * ; = / j * j ; b = j / * ; printf( "%d %d %f %f", k, l, a, b ) ; } 

it simple c program yashwant kanetkar not relate answer . if compile program output getting

0 2 0.00000 2.00000 

this simple program not able explain output may getting confused associativity. both / , * have l r associativity , / has unambiguous left operand (necessary condition l r associativity) performed earlier. answer different in case .

it simple associativity of operators, nothing complex.

i think it's "integer division" property making confused.

  1. k = / j * j ; answer 0, because of integer division first (i / j == 0).
  2. l = j / * ; answer 2, because of integer division first. (j / i == 1)
  3. a = / j * j ; answer 0, (promoted float) because of integer division first, mentioned earlier.
  4. b = j / * ;, answer 2, (promoted float) because of integer division first, alo mentioned earlier.

note: a gentle version of mr. haccks said : please avoid referring particular book.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -