Someone could tell me if this code:
int a = 1;
int b = 0;
if (likely(a >= b))
return 1;
make the same this other?
return (likely(a>= b))
thank so much for the help.
Someone could tell me if this code:
int a = 1;
int b = 0;
if (likely(a >= b))
return 1;
make the same this other?
return (likely(a>= b))
thank so much for the help.
likely
is not part of the C language. It must be defined somehow by the context of the code where you saw this. (It is probably a macro that expands to a compiler-specific construct that tells the compiler that a >= b
is likely to be true, which can help optimization. But from the code you showed, we do not know that for sure.)
Assuming that likely
is indeed such a macro, we can remove it and proceed to your actual question:
if (a >= b)
return 1;
is not the same as
return a >= b;
because they do different things when a is less than b. return a >= b;
returns zero when a is less than b, but if (a >= b) return 1;
goes on to whatever is below the if-statement when a is less than b.
Two constructs that are equivalent to return a >= b;
are
if (a >= b)
return 1;
else
return 0;
and
if (a >= b)
return 1;
return 0;
Most experienced C programmers would rather see return a >= b;
than either.
[Addendum: return likely(a >= b);
is a peculiar thing to write, because the function is going to return either way, and the code setting up the return value is short, simple, and (often) branch-free. It could conceivably provide a hint to interprocedural optimization, but it would be more natural to express that kind of hint as an annotation on the function prototype, e.g.
extern int fstat(int fd, struct stat *st)
__attribute__((expected_return_value(0)));
(Note: not a real GCC function annotation as far as I know.)]
They're not the same, because the first block of code doesn't return immediately when a >= b
is false, it continues on with the rest of the function (which you haven't shown). So unless the next line is return 0;
, they're different.
With the values of a
and b
that you give, the whole block of code is simply equivalent to return 1;
. But I assume that it's possible for a
and b
to have different values.