if (-1) returns true

C

CBFalconer

Nicholas said:
CBFalconer said:
Especially since this is way OT for c.l.c, this gross error needs
correction. Pascal always defined booleans as the enumeration
false, true. Check Jensen & Wirths "Pascal Report and User Manual"
if you don't believe me.

You might also want to check to see what the compiler actually does.
The (D5 Pro UP1) compiler produces two different output depending on
the check of true is being done.

If you have this kind of comparision happening:

if SomeFunction() = true then
begin
end;

then the compiler will test to see if the result from SomeFunction()
is equal to 1 ($[00]01).

However, if you have this kind of comparision happening:

if SomeFunction() then
begin
end;

then the compiler will test to see if the result from SomeFunction()
is not zero!

If SomeFunction does not return a boolean (see 1st para. above) you
should have a syntax error, and the thing just doesn't compile. I
can't speak for compilers that don't implement Pascal. If you want
to test something for non-zero all you have to do is say so:

IF junkfunction <> 0 THEN ...

This is way OT for c.l.c, so I am setting f'ups. I won't see any
reply. BTW, Delphi does not implement Pascal. At least these days
they have the grace not to claim it does. Pascal has an ISO
standard.
 
R

Richard Bos

ATM Dude said:
I love that about C, you can test any variable as if it were a boolean.

Well... any _scalar_ type. You can't do this with structs and unions,
and while you can try it with arrays, it's a bit senseless to write a
test that will, because of a quirk in C's array handling, always
evaluate to true.

Richard
 
R

Richard Bos

Original C doesn't
even have a Boolean type and the fact that one can find different values for
True in existing published libraries says to me that there is still debate
in that community over an appropriate value for True.

You're both wrong about C. In a context where a boolean value is
required, any non-zero scalar will be taken as true. In a context where
a boolean value results, true will be returned as an int with value 1.
Thus, for example, 4.0 && !NULL equals 1. Any library which doesn't cope
with this doesn't cope well with ISO Standard C, or with any variation
thereon that I've ever used.
Note that in C99, which has a _Bool type built in, and a <stdbool.h>
header which defines boolean, false and true if you want them; true is
an integer 1.

Richard
 
M

Mark McIntyre

Not according to Michael Mair. His post indicates that <stdbool.h> has to be
included. Presumably there is only one <stdbool.h> ;).

I didn't see michael's post, but you do not need to include stdbool.h to
get _Bool since its an intrinsic type.

The header defines an macro bool which expands to _Bool, and two macros
true and false which expand to 1 and 0 respectively.
 
W

Walter Roberson

:pascal always defined booleans as the enumeration
:false, true.

Pascal enumerations formed their own type, and the values need
not be sequential as long as the ord() function knows the mapping.
 
J

Jamie

ATM said:
I love that about C, you can test any variable as if it were a boolean.

if(somechar){
// Do your thing
}

or

if(somebyte){
// Ditto
}
also with delphi with a little help.
if LongBool(Integervalue) then...
 
C

CBFalconer

Walter said:
Pascal enumerations formed their own type, and the values need
not be sequential as long as the ord() function knows the mapping.

It's still OT on c.l.c. F'ups set.

Pascal enumerations have no specific value. However the ord
function can reveal their place in the enumeration, and the succ
and pred functions can select adjacent values. The enumeration is
a grown up independant type in its own right, unlike the C flavor.
 
N

Nicolai Hansen

Skybuck Flying said:
Hi,

I came across this C code which I wanted to understand etc it looked like
this:

if (-1) etc

It made me wonder what the result would be... true or false ?

In C and Delphi

False is defined as zero.
True is defined as non zero.

I find this a bit weird since false and negative and true and positive seem
more intuitive.

Think of a lieutenant who asks a question to a soldier, the soldier will
reply:
"Negative" if it's not true.
"Affirmitive" if it's true.

Therefore wouldn't this be more intuitive ?:

A Boolean is defined as a "byte" which is unsigned. -1 is 255,
positive, TRUE (or rather, non-false).
False defined as negative or zero.
True defined as positive.

You can do that if you want, wont make a difference for unsigned
integers anyway :)
 
K

Keith Thompson

A Boolean is defined as a "byte" which is unsigned. -1 is 255,
positive, TRUE (or rather, non-false).

Keep in mind that this is (inappropriately) cross-posted to
alt.comp.lang.borland-delphi and comp.lang.c. I presume your
description applies to Delphi; it's inaccurate for C.

Followups redirected. (My news server doesn't seem to carry
alt.comp.lang.borland-delphi, so I probably won't see any followups.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,421
Latest member
StacyTaver

Latest Threads

Top