Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected C behaviour

I'm trying to understand why my program won't to compile,

int myfunction(int x)
{
   return x;
} 

int main(){
  int x = 10;
  int result=0;
  result=myfunction(x) * myfunction(++x);
  printf("Result is = %d", result);
}

After execution i get : warnings being treated as errors In function 'int main()': operation on 'x' may be undefined. Someone has an idea ?

like image 483
timmz Avatar asked Jul 05 '26 23:07

timmz


1 Answers

myfunction(x) * myfunction(++x) is undefined because the order of evaluation of the two arguments to operator * is unspecified. So either the first or second call can be executed first, meaning that theoretically x or ++x can be evaluated first, which can lead to different results. Theoretically. In practice, the standard just passes responsibility to you not to do this.

like image 189
Luchian Grigore Avatar answered Jul 07 '26 11:07

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!