Thread: boolean
View Single Post
  #3   Spotlight this post!  
Unread 28-12-2004, 00:28
jude's Avatar
jude jude is offline
Registered User
#0116 (Epsilon Delta)
Team Role: Mentor
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Herndon, Virginia
Posts: 3
jude is on a distinguished road
Send a message via AIM to jude
Re: boolean

You can get rid of the #defines by using an enum:
Code:
typedef enum {TRUE=1, FALSE=0} bool;
There's also a small practical benefit for this approach:

Some compilers will be able to do some extra type-checking against the enum values, and warn you if you accidentily try to do something like "bool b_Var = 5".


Quote:
Originally Posted by Joe Ross
There is no boolean variable type in C. Most people declare it as char (or unsigned char) and use the value 0 for false and 1 (or any value != 0) for true.

Through the use of #defines and typedefs, you can emulate it, though.

Code:
typedef unsigned char bool
#define TRUE 1
#define FALSE 0
Then just use the variable type bool and the values TRUE and FALSE.