• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operators in c

 
aaka jain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void main(){
int a;
a=10;
a*=10+2;
printf("%d",a);
}

it is giving me output as 120,as multiplication has higher priority than addition so i think output should be 102,can sonebody please explain me why i m getting output as 120,thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiplication has higher precedence than addition. But addition has higher precedence than assignment, and *= is considered an assignment operator.
 
Mike Blaszczak
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The combined operators, like += and *=, can always be thought of as treating their right hand operator as a grouped expression. That is, "a *= 10 + 2" is equivalent to "a = a * ( 10 + 2 )", and is not equivalent to "a = a * 10 + 2".

 
chuck morris
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the point is that you should not forget about associative here(association > precedence ( ) ) . X= has right to left association .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic