Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: main return value is helpfull? Date: Tue, 18 Apr 2017 09:29:40 -0700 Organization: None to speak of Lines: 34 Message-ID: References: <7e8ec0c1-c5ac-4657-8c7f-5c26c5bf0c74@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="edf5e827066e2c2c4b7458be22116fd1"; logging-data="19852"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193FyPRDIOyRyKm516fusWQ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:HhM6uyJRndqkHKAu2Kvsg5wn9xo= sha1:gAa7Z5aSRpPCNDo3sHxWoyzsox4= Xref: csiph.com comp.lang.c:108279 Manfred writes: > On 04/17/2017 09:19 PM, Keith Thompson wrote: >>> 2) When explicitly tested, a return value of zero (0) is treated as an >>> indication of success. A non-zero value is/not/ an explicit indication >>> of failure. >> It depends on how you test it. For example, the shell's "if" statement >> treats a return value of 0 as success/true, and any non-zero return >> value as failure/false. > This is probably best expressed as the shell, when converting to > boolean, mapping zero to true, and nonzero to false. The notion of > success/failure might suggest consequences that are in fact left under > control of the script writer. The way I'd describe it is that the shell's "if" statement (unlike C's "if" statement) makes its decision based on whether the command succeeds or fails rather than truth vs. falsehood. That's why there's a "true" command that always succeeds,and a "false" command that always fails, along with a "test" command that evaluates an expression and then succeeds if it's true or fails if it's false. If you have a true/false condition, you have to convert it to a success/failure status in order to use it with "if". The shell's use of zero for success and any non-zero value for failure can be a little confusing, since it's seemingly the opposite of C's use of zero for false and non-zero for true. The reason is that typically there's only one way for a command to succeed, and multiple ways it can fail. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"