LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-30-2010, 06:10 AM   #1
tadeas
Member
 
Registered: Jul 2008
Location: Prague
Distribution: Opensuse
Posts: 40

Rep: Reputation: 5
C bool return values conventions


Hi!
I have a function that returns a boolean value, True of False. Is there any convention whether 0 = False or 0 = True?
It should be obvious, 0=False, 1=True. However programs usually return 0 on success and reserve higher numbers for error values. I.e. if the program finishes successfully (function int main() finishes successfully ), it returns 0 - and that contradicts that 0 should be false.

And especially if I want a function to do some computations on parameters passed by reference and return success status. Should it return 1 (true) for success, or should it return 0 for success - like any Unix program does?

Is there any convention for this?
 
Old 10-30-2010, 07:47 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The mix of conventions is a bit counter-intuitive, but the conventions are:

Program success = 0
Program other status = non zero

Within a program false = 0
Within a program true = non zero

Strict bool false = 0
Strict bool true = 1

Quote:
Originally Posted by tadeas View Post
if I want a function to do some computations on parameters passed by reference and return success status. Should it return 1 (true) for success, or should it return 0 for success - like any Unix program does?

Is there any convention for this?
Unfortunately, for function success/failure there is no well recognized convention.

How many different success values might you need? How many different failure values might you need? Someone apparently decided that for Unix program exit status only one success value was needed and multiple non success values.

If you only need one failure value, I find code a lot more readable if false is failure where tested by the calling function, so I prefer 0 as a fail code.

Last edited by johnsfine; 10-30-2010 at 07:53 AM.
 
  


Reply



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
Inconsistency in function return values Completely Clueless Programming 5 09-03-2009 09:12 AM
system() and return values PatrickNew Programming 1 02-07-2007 04:35 AM
How to use return values from system functions? kpachopoulos Programming 2 07-30-2004 03:09 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM
How can get values return from a system call quenn Programming 1 09-16-2003 11:05 PM

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

All times are GMT -5. The time now is 12:41 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
Open Source Consulting | Domain Registration