Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3989

Re: boolean usage

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: boolean usage
Date 2011-05-14 16:41 -0700
Organization None to speak of
Message-ID <lnk4dshmty.fsf@nuthaus.mib.org> (permalink)
References <1e11781e-486c-414d-acbc-7d6447000270@f9g2000vbz.googlegroups.com>

Show all headers | View raw


"dr.oktopus" <blindwilly@freeonline.zzn.com> writes:
> I am developing a little library and I would like to develop it so
> that it will be conforming both to the old and the new (c99) standard.
> I have a function returning a boolean value, so I think that something
> like:
>
> file.h:
>
> #if __STDC_VERSION__ >= 199901L
> #include <stdbool.h>
> #else
> #define bool int
> #endif
>
> /* rest of the header */
>
> bool my_func (void);
>
> file.c:
>
> #if __STDC_VERSION__ >= 199901L
> #define false 0
> #define true (!false)
> #endif
>
> should be. But bool could be defined even in user code.
> What's the proper way to handle this kind of things?

Well, code that doesn't use _Bool or <stdbool.h> can still conform to
both C90 and C99 (the C committee carefully designed it that way).  But
if you want to use <stdbool.h> if it's available, perhaps with the goal
of dropping pre-C99 support eventually, there are several ways to do it.

The first thing I recommend reading is section 9 of the comp.lang.c FAQ,
<http://c-faq.com/>.

I'd probably use typedef rather than #define for the bool type, but
that's a fairly minor point.

I'm not sure why you define "bool" in file.h and "true" and "false" in
file.c.  And there's no point in defining true as (!false); 1 and 0 are
clear enough.

Here's how I might do it:

#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#else
typedef enum { false, true } bool;
#endif

The enum isn't *exactly* equivalent to C99's _Bool/bool, but it's not
hard to restrict your usage to cases where it's close enough.

On the other hand, as you say, the names "bool", "false", and "true"
might be defined in some other header and your own names could conflict
with them.  In that case, you can pick a unique prefix and do something
like:

#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#define FOO_bool bool
#define FOO_false false
#define FOO_true true
#else
typedef enum { FOO_false, FOO_true } FOO_bool;
#endif

That's quite ugly; you'll have to decide whether it's worth it.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

boolean usage "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-05-14 14:05 -0700
  Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-14 16:41 -0700
    Re: boolean usage Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-15 04:18 +0100
  Re: boolean usage "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-05-14 23:12 -0700
  Re: boolean usage Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-05-14 23:39 -0700
    Re: boolean usage "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-19 21:29 -0400
      Re: boolean usage Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-05-19 22:49 -0700
        Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-20 09:17 -0700
          Re: boolean usage gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-20 17:14 +0000
  Re: boolean usage Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-15 10:58 -0400
    Re: boolean usage Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-05-15 08:12 -0700
    Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-16 08:09 -0700
      Re: boolean usage Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-16 19:44 -0400
        Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-16 17:46 -0700
          Re: boolean usage Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-16 22:45 -0400
  Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-16 18:23 -0700
    Re: boolean usage Jens <jens.gustedt@gmail.com> - 2011-05-16 23:37 -0700
      Re: boolean usage James Kuyper <jameskuyper@verizon.net> - 2011-05-17 06:39 -0400
        Re: boolean usage "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-05-17 09:55 -0400
          Re: boolean usage James Kuyper <jameskuyper@verizon.net> - 2011-05-17 21:17 -0400
      Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-17 16:50 -0700
        Re: boolean usage Jens <jens.gustedt@gmail.com> - 2011-05-18 09:21 -0700
          Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-18 15:36 -0700
            Re: boolean usage Jens <jens.gustedt@gmail.com> - 2011-05-19 00:20 -0700
              Re: boolean usage Ralf Damaschke <rwspam@gmx.de> - 2011-05-19 09:04 +0000
            Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-19 01:13 -0700
              Re: boolean usage Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-05-19 03:29 -0700
              Re: boolean usage Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-19 11:41 +0100
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-19 11:24 -0700
                Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-19 11:55 -0700
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-19 14:40 -0700
                Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-19 15:48 -0700
                Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-23 18:26 -0700
                Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-23 18:29 -0700
                Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-23 18:27 -0700
                Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-24 00:11 -0700
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-24 00:48 -0700
                Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-24 09:16 -0700
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-24 10:10 -0700
                Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-24 10:48 -0700
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-25 10:18 -0700
                Re: boolean usage gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-19 21:53 +0000
                Re: boolean usage Michael Press <rubrum@pacbell.net> - 2011-05-25 11:22 -0700
                Re: boolean usage Kenneth Brody <kenbrody@spamcop.net> - 2011-05-25 16:10 -0400
                Re: boolean usage Michael Press <rubrum@pacbell.net> - 2011-05-26 12:32 -0700
                Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-19 22:39 -0700
                Re: boolean usage Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-20 12:57 +0100
                Re: boolean usage Peter Nilsson <airia@acay.com.au> - 2011-05-22 17:52 -0700
                Re: boolean usage Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-23 13:15 +0100
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-24 00:57 -0700
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-24 01:38 -0700
                Re: boolean usage Keith Thompson <kst-u@mib.org> - 2011-05-24 09:20 -0700
                Re: boolean usage Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-24 11:00 -0700

csiph-web