bool in c

Started by
9 comments, last by Nodger 19 years, 11 months ago
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
Advertisement
this doesn''t answer your question, but why not just:

typedef enum {false, true} bool;
I found the problem...dumb, (the typedef should have been
typedef  unsigned long  bool; 
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>
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 0main(){  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]
Unless your version of C is over 15 years old, relational operators are required to return 0 or 1.
CoV
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.
Alright, bad example. Take this one:


if(isdigit(''5'') == true)
printf("TRUE");
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.
My last post was in response to Mayrel. we had a little race condition. you must have snuck in there just before i posted.

This topic is closed to new replies.

Advertisement