简体   繁体   中英

what is the correct way to compare the return of a function

A simple question. I have a function int function(void) . Are the following statements the same:

a. if( function() == somevalue ) //somevalue is integer
b. if( (function()) == somevalue )
c. if( (ret = function()) == somevalue ) //where ret is an integer

All three of them are functionally equivalent as far as comparing the return value is concerned.

(a) and (b) are equivalent whereas (c) lets you save the return value from function (in ret ) if you need later. Unless you need the return value of function() later for some other purpose, I'd recommend using (a).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM