Thread: Int return from functions - 0 or 1 ?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    28

    Int return from functions - 0 or 1 ?

    Most of the standard C system calls I use return 0 on success, -1 on failure and sometimes an arbitrary positive int as information to the caller.

    This means comparing to -1 to check on success, eg:-

    Code:
    if (thatWorked != -1) printf("Hooray!");
    I personally find returning a 1 (true) on success and 0 (false) on failure makes for easier to read code:-

    Code:
    if (thatWorked) printf("Hooray!");
    .... but I'm trying to adhere to common standards as much as possible, and having some functions return 1 on success and some return 0 on success makes for even worse code reading.

    Any thoughts? What style do you use?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Usually things can only succeed in one way and fail in multiple ways. In these cases 0 is used to indicate success and any other number to indicate various error conditions.

    In your upper example, if you are really interested in success, that's what you should test against (thatWorked == 0). And if your functions only "fail" in one way it would make more sense to return 1 for success and 0 for error.

    In the end you'll have two kinds of functions: ones that return true or false and ones that provide more information than that. I mean, something like OpenDatabase clearly might fail in several ways whereas IsPrime is the kind of function that is supposed to return a boolean.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by trillianjedi View Post
    Most of the standard C system calls I use return 0 on success, -1 on failure and sometimes an arbitrary positive int as information to the caller.
    Normally there are more ways for something to fail than for it to succeed. Typical approach is that of HRESULTs:
    0 == SUCCESS
    < 0 == FAILED
    > 0 = SUCCEEDED with extra info (Normally very few of these exist, only S_FALSE that I'm aware of)
    This is a good approach because comparisons with zero are usually simpler for the CPU.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The only good thing about 0 for success is using:
    Code:
    if (foo()) {
       printf("error");
       exit(1);
    }
    if 1 for success then you would need if (!foo()). An extra ! :P.
    But yeah, having the obvious 0 for failure is more important. You can use 0 and nagatives for failure, positives for success.
    Also the above code is kind of "ugly". I mean at a first glance it is misleads you to understand "if foo() succeeds then print error and exit".
    So just for these two reasons I would personally prefer 1 for success.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could also do:
    Code:
    if ( succes(foo()) )
    {
       printf("error");
       exit(1);
    }
    And have foo return something other than 0 or 1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by C_ntua View Post
    The only good thing about 0 for success is using:
    Code:
    if (foo()) {
       printf("error");
       exit(1);
    }
    if 1 for success then you would need if (!foo()). An extra ! :P.
    But yeah, having the obvious 0 for failure is more important. You can use 0 and nagatives for failure, positives for success.
    Also the above code is kind of "ugly". I mean at a first glance it is misleads you to understand "if foo() succeeds then print error and exit".
    So just for these two reasons I would personally prefer 1 for success.
    That's misleading. There is no point in returning an error code, if you don't output what it is. The code that you have up there is pretty pointless. Normally, I'd write:
    Code:
    int error_code = foo();
    if (error_code) {
      printf ("Error received from foo. Error code = %d\n", error_code);
      return 1;
    }
    /* Do something else */
    return 0;
    That way rcode returns true when there is an error, making the whole interface nice and neat again. Besides which, you should always print out the error code that you receive.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, that is a better way to do it.
    I just have seen a lot of times the kind of code I posted. A programming language should try to prevent users from writing something misleading

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by C_ntua View Post
    A programming language should try to prevent users from writing something misleading
    A programming language should do the job it was designed for. C was not designed to have type-safety, so it doesn't prevent users from doing horrible things with casting. You can't say a language is good or bad just because it has or hasn't got a certain feature.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why not? No language is perfect. And the question is not about the language as a language, but about developing functions that return 0 for a success. That is something the guys that developed the functions decided. For me it would be better if they had decided to put 1 for success. That is just an opinion.

    And a language IS judged from it's features. That is why C has "evolved" in C, C++, C# becaused it lacked some useful features. We still use it because it still has some advantages and personally I prefer it from the rest. But even C changes slightly. Like the C's gnu '99 standard lets you declare more freely variables, which is a nice feature.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    For me it would be better if they had decided to put 1 for success. That is just an opinion.
    Well, no one's stopping you. It certainly isn't embedded in the language, so we're free to do what we want.

    We still use it because it still has some advantages and personally I prefer it from the rest.
    I don't see how ANYONE can like C over other higher-level languages such as C++. C++, being built upon C, does everything the C way and much more without sacrificing anything. Indeed, I would say that C++ can be as fast or faster than C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As discussed, there are several possible scenarios. Windows and Unix/Linux have slightly differnet strategies, and neither is entirely consistent. The C library has a further different (and inconsistent) standard.

    The reason for the inconsistency is generally that we want to use the return value for two purposes:
    1. Indicate success/failure.
    2. Pass some sort of data back to the program itself.

    An example would be fread, which returns a zero or positive value on "success", as it returns the number of items read, where zero means that it was not possible to read any more data items. A negative value (typically -1, EOF) is returned on an attempt to read "past the end of file", but also on other failures (invalid file-pointer, file has been closed before reading, etc).

    The Unix/Linux OS API function read() follows the same type of pattern: positive value -> successfully read x bytes.

    On the other hand, the Windows API ReadFile() returns a BOOLEAN that is TRUE (non-zero) for success and FALSE (zero) for failure.

    It is good if a group of functions (at the very least) follow a systematic pattern, and if all functions are following a pattern, it's even easier to write code tha tuses those functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by C_ntua View Post
    That is something the guys that developed the functions decided.
    Well that is not strictly true. C behaves the same way as UNIX, where a return code of 0 signals success. I suppose it was only natural to continue the same convention in C.
    But you only want function to return 1 for success because C sees 1 as boolean TRUE and 0 as boolean FALSE. In effect this whole debate only arises, because C has no built-in boolean type and we have to make do with a number instead. We then impose an arbitrary booleanness on the numbers.
    That, to me, is the bigger failing. Even FORTRAN has a "logical" type that can be used to return success or failure for a function.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    53
    It should be apparent from the name of the function, too. If a function is called "thatWorked()" then I would expect it to return non-zero on success, because that would be easier to read. If you look at C functions like islower() and isdigit(), they return non-zero if their argument is, respectively, a lower case letter or a digit.

    The first function new C programmers run into where this is an issue is usually strcmp(), because they mentally parse it as "is equal" and not as "string compare".

    --
    Computer Programming: An Introduction for the Scientifically Inclined

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM