Galladite
Galladite

Reputation: 137

Comparing x and x=f(x) in c

In C, how can I compare a variable to the result of a function, which I then want to store within the variable for later use? With the code shown below, it always returns true where f is a function that just returns x-1.

Something like this:

if (x == (x = f(x))) ...

Upvotes: 0

Views: 59

Answers (1)

ikegami
ikegami

Reputation: 385887

Type old_x = x;
x = f(x);
if ( x == old_x ) {
  ...
}

or

Type new_x = f(x);
if ( new_x == x ) {
  ...
} else {
   x = new_x;
}

Upvotes: 1

Related Questions