LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2013, 11:11 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,873

Rep: Reputation: 55
Why 'return 0'?


Hi: Language C: 0 is false, !=0 is true. In fact true is 0xffff...f, any number of efs, because it is all ones in binary. OK. For a regular function I return with 'return 0'. So, if f is a funcion, a= f is a= false. Why?
 
Old 03-23-2013, 11:32 PM   #2
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,873

Original Poster
Rep: Reputation: 55
It's an exit code!
 
Old 03-23-2013, 11:59 PM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 873

Rep: Reputation: 207Reputation: 207Reputation: 207
Usually the high bit is reserved for the sign (signed data types being more usual).

OK
 
Old 03-24-2013, 10:23 AM   #4
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 123Reputation: 123
It makes perfect sense if you regard the exit code as an ENCOUNTERED_AN_ERROR flag. (As a bonus, the return value indicates which error was encountered).

Last edited by psionl0; 03-24-2013 at 10:24 AM.
 
1 members found this post helpful.
Old 03-24-2013, 12:46 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,873

Original Poster
Rep: Reputation: 55
It's clear. As in
Code:
if(some_function())
{
        /* print error message here */
        
        /* return an exit code */
        return 1;
}
some_function is called, but within an if. At the same time, the caller signals an error in the return clause.
 
Old 03-24-2013, 07:13 PM   #6
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 123Reputation: 123
Quote:
Originally Posted by stf92 View Post
Code:
if(error_code = some_function())
{
        /* print error message here */
        
        /* return an exit code */
        return error_code;
}
Assuming that some_function() returns sensible error codes, it would make more sense to return that value rather than the generic "1".

Quote:
Originally Posted by stf92 View Post
some_function is called, but within an if. At the same time, the caller signals an error in the return clause.
some_function is always called in this code. Only the statements in the curly braces are conditionally executed.

Last edited by psionl0; 03-24-2013 at 07:18 PM.
 
Old 03-24-2013, 07:20 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,873

Original Poster
Rep: Reputation: 55
Of course. Thank you for you useful posts. psionl0.

"some_function is always called in this code. Only the statements in the curly braces are conditionally executed". No doubt about it.

Last edited by stf92; 03-24-2013 at 07:21 PM.
 
Old 03-25-2013, 01:22 PM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 463

Rep: Reputation: 192Reputation: 192
Quote:
Originally Posted by stf92 View Post
In fact true is 0xffff...f, any number of efs, because it is all ones in binary.
That's incorrect.

Quote:
Originally Posted by stf92 View Post
For a regular function I return with 'return 0'. So, if f is a funcion, a= f is a= false. Why?
For a regular function you can use whatever API you want. Functions like open() for instance return -1 on error and non-negative value when the succeeded. The fact that main() returns 0 on success and non-zero on failure comes from the fact that the value is interpreted by the environment when the function exits and on those 0 is success and non-zero is an “error code”.
 
3 members found this post helpful.
Old 03-25-2013, 11:32 PM   #9
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 123Reputation: 123
Quote:
Originally Posted by mina86 View Post
That's incorrect.
Yes, C defines TRUE to be 1 and not -1. It stems from the early days of C programming when the results of boolean tests were frequently used in arithmetic computations.

eg:
Code:
y = ((x == 5) * some_other_computation(x));
Such clever tactics are poor programming practice though. It is much clearer to use
Code:
if (x == 5) 
    y = 0;
else
    y = some_other_computation(x);

Quote:
Originally Posted by mina86 View Post
For a regular function you can use whatever API you want. Functions like open() for instance return -1 on error and non-negative value when the succeeded. The fact that main() returns 0 on success and non-zero on failure comes from the fact that the value is interpreted by the environment when the function exits and on those 0 is success and non-zero is an “error code”.
open() returns the file descriptor on success but to avoid using 0 to indicate failure (when exit uses it to indicate success) it returns -1 instead since file descriptors are always positive numbers. This illustrates a limitation of C in that it can only return a single value or a pointer to a structure (or a structure itself nowadays). The latter option would be unnecessarily complex.

Last edited by psionl0; 03-25-2013 at 11:35 PM.
 
Old 03-26-2013, 09:01 AM   #10
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 463

Rep: Reputation: 192Reputation: 192
Quote:
Originally Posted by psionl0 View Post
Yes, C defines TRUE to be 1 and not -1.
C does not define TRUE at all. C99 defines _True keywoard and to makes things easier true macro when including stdbool.h, but there is no TRUE. It is correct however that boolean operators return 0 or 1.

Quote:
Originally Posted by psionl0 View Post
open() returns the file descriptor on success but to avoid using 0 to indicate failure (when exit uses it to indicate success) it returns -1 instead since file descriptors are always positive numbers.
File descriptors are not always positive numbers. Standard input is zero which is not positive.

Quote:
Originally Posted by psionl0 View Post
This illustrates a limitation of C in that it can only return a single value or a pointer to a structure (or a structure itself nowadays). The latter option would be unnecessarily complex.
Functions can return structures since at least C89. I'm not sure how you define “nowadays”.
 
Old 03-27-2013, 07:18 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,503

Rep: Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080
To summarize: always read the documentation of the actual function.
Examples:
malloc, fopen: NULL=error
open: -1=error
read: -1=error,0=eof
isatty: 1=yes,0=no
inet_aton: 0=error
 
1 members found this post helpful.
Old 03-27-2013, 07:31 AM   #12
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1289Reputation: 1289Reputation: 1289Reputation: 1289Reputation: 1289Reputation: 1289Reputation: 1289Reputation: 1289Reputation: 1289
Quote:
Originally Posted by mina86 View Post
For a regular function you can use whatever API you want. Functions like open() for instance return -1 on error and non-negative value when the succeeded. The fact that main() returns 0 on success and non-zero on failure comes from the fact that the value is interpreted by the environment when the function exits and on those 0 is success and non-zero is an “error code”.
I agree, this is the way it has to be to make sense. The return value has to be interpreted.
 
Old 04-02-2013, 02:38 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.3
Posts: 17,524

Rep: Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417Reputation: 2417
There's also the conceptual approach; there's a variable number of ways a fn can fail (& why not supply a different value for each one), but you only need one value for success ...
 
Old 04-02-2013, 03:24 AM   #14
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 123Reputation: 123
Quote:
Originally Posted by chrism01 View Post
There's also the conceptual approach; there's a variable number of ways a fn can fail (& why not supply a different value for each one), but you only need one value for success ...
That's pretty much what I said in the beginning (0 = no failure). However, there are a number of ways to succeed (eg return the address of memory that was allocated).

In the absence of returning a structure that includes an error code, a single return value has to include some way of indicating error codes as well as success codes.

So, what H_TeXMeX_H said ...
 
Old 04-02-2013, 08:19 PM   #15
bloody
Member
 
Registered: Feb 2013
Location: Berlin
Distribution: Gentoo, Debian
Posts: 172

Rep: Reputation: 25
If a C/C++ function states to use boolean return codes, then 0 will be false. I always test against 0 (false) and treat anything else as true, no matter if stdbool.h, C++, GTK or whatever, doesn't matter if true is defined as 1 (the common standard for C/C++) or even -1. Just treat 0 as false and you're good. Still, check if the API you're using is defining an own boolean type and then use that type.

Other functions return a different return value type (other than bool, e.g., int), sometimes using a return code of zero for success and a range of other codes for multiple different outcomes, so they can pass additional information in the returncode.

A software that exits (e.g., back to the shell it was launched from) either returns EXIT_SUCCESS (0) or some kind of error/failure returncode. Not boolean either.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
And so I return talkToTheHat LinuxQuestions.org Member Intro 1 07-01-2010 09:05 PM
dpkg return error :post installation script return an error code (1) grimfold Debian 2 09-10-2009 01:55 PM
return value neerajchaudhari Linux - Newbie 1 08-29-2005 01:42 AM
return 0 or return(0) in C? servnov Programming 5 01-08-2005 04:39 PM
return linuxanswer Programming 4 10-25-2003 11:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration