✔ 最佳答案
That should work just fine. A shorter way to write it is to use the ?: conditional operator:
return x > 0 ? true : false;
An expression of the form A ? B : C will evaluate expression A. If it is nonzero ("true") then B will be the result and expression C will not be evaluated. Otherwise, C will be the result and expression B will not be evaluated.
Some programmers prefer to put the condition (the "A" part) in parentheses:
return (x > 0) ? true : false;
That helps if you're not used to C's operator precedence rules ("order of operations").
If you are using the bool, true and false definitions from <stdbool.h>, the code is even easier:
return x > 0;
The > operator returns an integer 0 (false) if the test fails or 1 (true) if the test succeeds. There's no need to do anything else with it. If you want to emphasize that the result is a bool value, then:
return (bool)(x > 0);