Is it true that "0" is true under sh while false under C?
--
Jim> Is it true that "0" is true under sh while false under C?
Yes. The reason for this choice in sh is that the designers of UNIX
felt it more important to differentiate between various ways for a
program to fail than the ways it can succeed, so 0 was assigned as the
exit code for success while nonzero exit codes are used to signal
failure. And it makes more sense to let the exit code for success
stand for truth than falsehood in a shell program.
I don't know if there is an equally strong reason for the opposite
choice in C, except for convention (0=false, 1=true, though I have
seen books on mathematical logic using the opposite convention!)
--
Dept of Mathematical Sciences
The Norwegian Institute of Technology
N-7034 Trondheim, NORWAY
The question is simply one of whether you want to have many different
meaningful and interesting successful return values or whether you want to
have many failing ones. Some systems don't reduce to this representation,
like UNIX system calls, which have to rely on errno for their multiplicity
of failure modes, since the success modes are taken.
Also, it's not technically accurate to state that "0" is true under sh.
It depends on what you're trying to do. The built-in has to run a program.
Just saying
sh -c 'if [ 0 ]; then echo ok; else echo nok; fi'
sh -c 'if [ 1 ]; then echo ok; else echo nok; fi'
sh -c 'if 0 ; then echo ok; else echo nok; fi'
sh -c 'if 1 ; then echo ok; else echo nok; fi'
isn't very useful.
(Actually, you'll find that "0" is true in C. It's
0 that's false. :-)
--tom
--
"...we will have peace, when you and all your works have perished--and the
works of your dark master to whom you would deliver us. You are a liar,
Saruman, and a corrupter of men's hearts." --Theoden, from perl/src/aint.c
Andy.
: >Is it true that "0" is true under sh while false under C?
: >--
: This could develop into a quite long discussion about the
: semantics of truth. You can define true to be anything you like.
: For instance strcmp, strncmp both return 0 when true, but could
: just as easily have been written to return 1. isupper, islower
: etc. all return 0 when false.
: Andy.
The comparison operations return the value of a lexical comparison of two
items as follows:
strcmp() Compares its arguments and returns an integer less
than, equal to, or greater than zero, depending on
whether s1 is lexicographically less than, equal to, or
greater than s2.
That is not the same as returning true or false. In C, a non-zero
value is taken as false, since a zero value (NULL) in pointers generally
equals failure (an NULL returned by fopen or malloc, for instance).
In the shells, a zero value is taken for true since a zero exit condition
provides the least information and negative exit conditions do not exist.
Where there are multiple failure conditions, testing the exit code can
discern among them.
Just to be pedantic.
--
Dan Mercer ClearSystems, Inc
======================================================================
All opinions expressed are my own and do not reflect the opinions of
my employer or my employer's clients, in particular 3M Company.
All advice or software offered or presented is provided As Is with no
warranty either expressed or implied. Follow at your own risk.
Objects in the mirror are closer than they appear.
?Is it true that "0" is true under sh while false under C?
Yes. sh uses negative logic, while csh uses positive logic.
negative logic: 0=True, 1=False
positive logic: 0=False, 1=True
It is dangerous to assume that someone will always use +ve logic.
Boolean arithmetic should state which type of logic it uses.
Liam.
--
8"88"""8P"48"V"8 Liam Crilly 8"88"""8P"48"V"8
++ Performance Benchmarking Unit, Client-Server Systems, ICL, Bracknell. ++
?Is it true that "0" is true under sh while false under C?
Yes. sh uses negative logic, while csh uses positive logic.
negative logic: 0=True, 1=False
positive logic: 0=False, 1=True
It is dangerous to assume that someone will always use +ve logic.
Boolean arithmetic should state which type of logic it uses.
Liam.
Why was this done? What possible purpse could 2 inconsistent logics serve?
> ?Is it true that "0" is true under sh while false under C?
> Yes. sh uses negative logic, while csh uses positive logic.
>Why was this done? What possible purpse could 2 inconsistent logics serve?
--
Paul Southworth
CICNet Systems Support
The ISO C functions strcmp and strncmp don't return truth values.
They return comparison results, one of three values. They return
0 when the comparison shows the strings to contain equal characters.
--
"If business is war, the computer business is thermonuclear war". Ted Lewis.
--
-----------------------------------------
-----------------------------------------
|>> For instance strcmp, strncmp both return 0 when true, but could
|>> just as easily have been written to return 1. isupper, islower
|>> etc. all return 0 when false.
|> If we're going to get technical then...
If we're going to get pedantically then...
|> The ISO C functions strcmp and strncmp don't return truth values.
|> They return comparison results, one of three values.
They may return one of more than three values (actually all the range
of -128 to 127, at least), but these values are divided into three
categories (< 0, == 0, > 0).
\end{off-topic}
--
Andreas Schwab "And now for something
Actually somebody at berkley thought it should be changed and so they changed
it. The idea of a 0 for true is because 0 means success in unix. Commands
return 0 generally when they are successful. For scripts this means
that some like if command; then xxx will work when the command succeeds.
If you think of success and failure instead of true and false you will do
much better.
--
_ _ Dale DePriest San Jose, California
o/_/ (_(_X_(` fax: (408) 943-0402
Whilst I agree that the values returned by the strcmp, strncmp
routines are not boolean truth values (as you say, look at the
man page) the point is that if you look at simply the statement
if (strcmp()), all you are interested in (usually) is that if the
two strings you are comparing are identical, you want a truth
result that you can use to determine what to do next, and so,
since strcmp returns 0 if they are identical, the answer to the
question - are these two strings identical? is 0 if true and
non-zero if false.
This of course is English semantics and not programming language
semantics. (Just being picky :) ).
Andy.
if (strcmp(x, y) == 0)
I translate this to "Is the diffence between x and y equal to none".
1. Xconfig File for Cirrus Logic CLGD-5424
I am having terrible problems getting X to work on my machine with my
Cirrus Logic CL-GD5424 card. I have tried using the readme.cirrus,
but all of the options that I have tried fail.
Thank you
Thomas Munn
2. Linux SNA
6. T3 Arrays
7. XF86Config for Cirrus Logic CLGD5430/5434 needed ...
9. Cirrus Logic AGP Locking Up, Help!
11. Trantor T130b SCSI Card / Cirrus Logic 5428 VLB Card
12. HELP! Cirrus Logic 546X card
13. Slackware X dist and Avance Logic Server err 11