Defining Boolean datatype! 
Author Message
 Defining Boolean datatype!

Hi,

Can I do the following in ANSI C to define a datatype?

typedef enum {FALSE, TRUE} boolean;

If not, how can I do it then?

Best regards,

--
Paulo J. Matos aka PDestroy
http://www.*-*-*.com/
ICQ UIN - 361853

--
Knowledge is gained by learning; trust by doubt; skill by practice; and love
by love.
           - Anonymous



Fri, 03 Oct 2003 22:30:25 GMT  
 Defining Boolean datatype!

Quote:
> Can I do the following in ANSI C to define a datatype?

> typedef enum {FALSE, TRUE} boolean;

Yes.

--
"The fact that there is a holy war doesn't mean that one of the sides
 doesn't suck - usually both do..."
--Alexander Viro



Fri, 03 Oct 2003 22:42:46 GMT  
 Defining Boolean datatype!

Quote:
> Hi,

> Can I do the following in ANSI C to define a datatype?

> typedef enum {FALSE, TRUE} boolean;

That will work, although I might use bool instead....it's still clear
what it is and you save three characters of typing.

--
== Eric Gorr ===== http://www.*-*-*.com/ :9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Fri, 03 Oct 2003 22:50:10 GMT  
 Defining Boolean datatype!
Paulo J. Matos aka PDestroy a crit dans le message

Quote:
>Can I do the following in ANSI C to define a datatype?

>typedef enum {FALSE, TRUE} boolean;

Yes, but what's wrong with 0 and 1? (Just trolling, hehe)

--
-hs-    "spaces, not tabs"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Fri, 03 Oct 2003 22:54:25 GMT  
 Defining Boolean datatype!

Quote:

> Paulo J. Matos aka PDestroy a crit dans le message

> >Can I do the following in ANSI C to define a datatype?

> >typedef enum {FALSE, TRUE} boolean;

> Yes, but what's wrong with 0 and 1? (Just trolling, hehe)

Pretty lame troll seeing as many people (including me) do prefer
to simply use 0 and 1.
--
"What is appropriate for the master is not appropriate for the novice.
 You must understand the Tao before transcending structure."
--The Tao of Programming


Fri, 03 Oct 2003 23:02:21 GMT  
 Defining Boolean datatype!
Eric Gorr a crit dans le message

Quote:

>> Hi,

>> Can I do the following in ANSI C to define a datatype?

>> typedef enum {FALSE, TRUE} boolean;

>That will work, although I might use bool instead....it's still clear
>what it is and you save three characters of typing.

I prefer the following solution:

<intentional blank line>

It saves 36 characters + 3 for each 1 instead of TRUE and 4 for each 0
instead of FALSE.

What do you think boolean expressions like (3==4) or (8!=9) return?

--
-hs-    "spaces, not tabs"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Fri, 03 Oct 2003 22:59:24 GMT  
 Defining Boolean datatype!

Quote:

> I prefer the following solution:

> <intentional blank line>

> It saves 36 characters + 3 for each 1 instead of TRUE and 4 for each 0
> instead of FALSE.

True, but then issues such as code clarity come into play and depending
on how one feels about those issues, having a boolean type defined can
be useful...and when it is useful, I'd rather not have to type in three
extra characters and simply define it as bool instead of boolean.

--
== Eric Gorr ===== http://www.*-*-*.com/ :9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Fri, 03 Oct 2003 23:24:30 GMT  
 Defining Boolean datatype!

|
|> I prefer the following solution:
|>
|> <intentional blank line>
|>
|> It saves 36 characters + 3 for each 1 instead of TRUE and 4 for each 0
|> instead of FALSE.
|
| True, but then issues such as code clarity come into play and depending
| on how one feels about those issues, having a boolean type defined can
| be useful...and when it is useful, I'd rather not have to type in three
| extra characters and simply define it as bool instead of boolean.

You mean like:

#define is_equal(a,b) ((a)==(b))
#define str_is_equal(a,b) (strcmp((a),(b))==0)

or maybe:

#define EQUALS ==

I've seen people even do this in the name of "clarity":

#define BEGIN {
#define END }

but I can bet you can guess where they came from SMILEY

oh, the above needs:

#define SMILEY :-)

--
-----------------------------------------------------------------
| Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |

-----------------------------------------------------------------



Sat, 04 Oct 2003 02:34:25 GMT  
 Defining Boolean datatype!

Quote:


> > I prefer the following solution:

> > <intentional blank line>

> > It saves 36 characters + 3 for each 1 instead of TRUE and 4 for each 0
> > instead of FALSE.

> True, but then issues such as code clarity come into play and depending
> on how one feels about those issues, having a boolean type defined can
> be useful...and when it is useful, I'd rather not have to type in three
> extra characters and simply define it as bool instead of boolean.

The C99 committee clearly agrees:

7.16 Boolean type and values <stdbool.h>
  1 The header <stdbool.h> defines four macros.
  2 The macro
        bool
    expands to _Bool.
  3 The remaining three macros are suitable for use in #if preprocessing
directives. They
    are
        true
    which expands to the integer constant 1,
        false
    which expands to the integer constant 0, and
        _ _bool_true_false_are_defined
    which expands to the integer constant 1.
  4 Notwithstanding the provisions of 7.1.3, a program may undefine and
perhaps then
    redefine the macros bool, true, and false.213)

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sat, 04 Oct 2003 03:19:29 GMT  
 Defining Boolean datatype!


Quote:
>Hi,

>Can I do the following in ANSI C to define a datatype?

>typedef enum {FALSE, TRUE} boolean;

>If not, how can I do it then?

You can do it however you like but since C99 adds support for a boolean
type it makes sense to to it in a way that it compatible with that.
C99 defines a header called <stdbool.h> which defines a type bool
macros false (as 0), true (as 1) and __bool_true_false_are_defined (as 1).

A program might use #include "stdbool.h" which would use the implementation's
version it it has one or the application could supply one for implementations
that don't have it.

The main issue is the type that is defined for bool. C99 has a specific
boolean type with some special properties (e.g. (bool)x is equivalent
to (bool)(x != 0). Code can't make use of this property if it can't
assume C99. bool is the lowest rank unsigned type in C99 so defining
it as unsigned char would probably be best in a user supplied header.

--
-----------------------------------------


-----------------------------------------



Sat, 04 Oct 2003 03:07:58 GMT  
 Defining Boolean datatype!

Quote:

> You mean like:

No.

--
== Eric Gorr ===== http://www.*-*-*.com/ :9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Sat, 04 Oct 2003 03:35:23 GMT  
 Defining Boolean datatype!

in comp.lang.c:

Quote:

> > Hi,

> > Can I do the following in ANSI C to define a datatype?

> > typedef enum {FALSE, TRUE} boolean;

> That will work, although I might use bool instead....it's still clear
> what it is and you save three characters of typing.

Using "bool" would be a BAD idea, now that it is defined in the C99
standard header <stdbool.h> as an alias for the built-in _Bool type.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Sat, 04 Oct 2003 11:35:16 GMT  
 Defining Boolean datatype!

Quote:

> Hi,

> Can I do the following in ANSI C to define a datatype?

> typedef enum {FALSE, TRUE} boolean;

> If not, how can I do it then?

You could, but it's silly.  0 and non-0 work quite nicely, and conform to what
the language thinks "false" and "true" are.  Even if you test
only the results of evaluations giving 1 for true, you have gained nothing and
for the general case to use this you will need to test if (x != FALSE)
rather than if (x == TRUE) where a simple if (x) would do.


Sat, 04 Oct 2003 14:24:00 GMT  
 Defining Boolean datatype!

Quote:


> > Hi,

> > Can I do the following in ANSI C to define a datatype?

> > typedef enum {FALSE, TRUE} boolean;

> > If not, how can I do it then?

> You could, but it's silly.  0 and non-0 work quite nicely, and conform to what
> the language thinks "false" and "true" are.  Even if you test
> only the results of evaluations giving 1 for true, you have gained nothing and
> for the general case to use this you will need to test if (x != FALSE)
> rather than if (x == TRUE) where a simple if (x) would do.

It's not silly, and thank goodness C99 disagrees with you.  No, you don't
want to compare boolean values explicitly against "true" and "false"--that
would be silly.  But, if "x" is meant to hold boolean values, then declaring
it as such and using assignments like "x = true" is quite reasonable.

-Jeff



Sat, 04 Oct 2003 15:07:44 GMT  
 Defining Boolean datatype!

Quote:

> Using "bool" would be a BAD idea, now that it is defined in the C99
> standard header <stdbool.h> as an alias for the built-in _Bool type.

Actually, it would be a great idea. Once you got a C99 compiler, it is
likely that all one would have to do is remove the user defined enum and
let the C99 definition take over.

--
== Eric Gorr =========================================== ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Sat, 04 Oct 2003 20:39:23 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Marshalling System.Boolean datatype.

2. how to define a boolean

3. boolean #define

4. DCOM Server - With User Defined Datatype - Crashes!

5. C++ Datatypes and OS Datatypes :: C++

6. #define defining other #defines

7. #define - cannot define/undefine preprocessor symbols after first token in file

8. #defines with #defines inside them?

9. #define with several #defines inside???

10. Preprocessor: defining a macro inside a #define directive

11. #define a define

12. #define REM /* #define ARK */

 

 
Powered by phpBB® Forum Software