Archived

This topic is now archived and is closed to further replies.

Nodger

bool in c

Recommended Posts

another C problem: I have defined a bool like so:
#define true  1
#define false 0

typedef bool (unsigned long);
 
everythings going fine except when I try the following:
bool foo;
foo=false;
 
I get an error saying an l-value is expected (as in the "foo" variable). Confusing. when I define foo as an unsigned long everything works fine

Share this post


Link to post
Share on other sites
quote:
Original post by Nodger
I get an error saying an l-value is expected (as in the "foo" variable). Confusing. when I define foo as an unsigned long everything works fine



It should be

typedef unsigned long bool

although my first instinct would be to recommend something smaller than a long to represent a bool. Or, if you have a C99 compliant compiler, just do

#include <stdbool.h>

Share this post


Link to post
Share on other sites
quote:

#define true 1
#define false 0



I'd recommend against that definition of true. True in C is non-zero, not necessarily one. A function that returns true might return 3 or 8 or 110 and still be compliant. Take this simple example.


#define true 1
#define false 0

main(){

int a = 5, b = 5;
if(isEqual(a, b) == true){
printf("equal")
}
}

int isEqual(int a, int b){
return a == b;
}


In that program, the print statement won't print. isEqual will return 5*, which is not equal to 1. A better definition of true and false is:

#define false 0
#define true !false

EDIT --

* i should say might return 5. I guess that's really compiler dependant. The point is that compiler would be ANSI-C compliant if it did return 5.


[edited by - kdogg on May 20, 2004 9:41:23 PM]

Share this post


Link to post
Share on other sites
Unless your version of C is over 15 years old, relational operators are required to return 0 or 1.

Share this post


Link to post
Share on other sites
quote:
Original post by kdogg
quote:

#define true 1
#define false 0



I''d recommend against that definition of true. True in C is non-zero, not necessarily one. A function that returns true might return 3 or 8 or 110 and still be compliant. Take this simple example.

That''s why you don''t make comparisons to true. This is "true" also in C++ when working with ints. Defining true as 1 is perfectly fine as long as you are using it for assignments not comparisons.

quote:
Original post by kdogg
A better definition of true and false is:

#define false 0
#define true !false


Your "better" definition doesn''t do anything different. Guess what value !0 yields? Besides that, even if for some reason it didn''t yield 1, you still would get one single value, in which case comparing to true still would not always give you the correct answer. In the end you are just left with the fact that you shouldn''t be comparing to true. You''re simply not going to find a way around that in C.

Share this post


Link to post
Share on other sites
quote:
Original post by kdogg
Alright, bad example. Take this one:


if(isdigit(''5'') == true)
printf("TRUE");

It appears you are missing the point entirely. Please reread my post.

Share this post


Link to post
Share on other sites
My last post was in response to Mayrel. we had a little race condition. you must have snuck in there just before i posted.

Share this post


Link to post
Share on other sites
True enough. Functions in the isdigit family return 0 or a random nonzero value.

So if you must pretend that C''s Boolean type is used by the standard library and by libraries designed to be compatible with older versions of C, then you''d have to cast the return value of isdigit to a _Bool (or bool if you''ve included stdbool.h).

if (((_Bool)isdigit(c)) == true)

Personally, I think if (isdigit(c)) is a whole lot nicer.

Share this post


Link to post
Share on other sites