Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 5 Dec 2011 13:16:31 -0500
From:      David Schultz <das@FreeBSD.ORG>
To:        mdf@FreeBSD.ORG
Cc:        Zack Kirsch <zack@FreeBSD.ORG>, John Baldwin <jhb@FreeBSD.ORG>, freebsd-arch@FreeBSD.ORG
Subject:   Re: Use of bool / stdbool.h in kernel
Message-ID:  <20111205181631.GA7393@zim.MIT.EDU>
In-Reply-To: <CAMBSHm-opYiyPqz0YVSYdOKHA7fbN=ZxOmMYC2Q_U4R7iFHOew@mail.gmail.com>
References:  <CAMBSHm_Be0hCimgg0KpCFs24MHOW=LBczJbFZ3F1cOaCgrS8LA@mail.gmail.com> <20111130154604.B949@besplex.bde.org> <201111301032.04102.jhb@freebsd.org> <20111201014944.GA78010@zim.MIT.EDU> <CAMBSHm-opYiyPqz0YVSYdOKHA7fbN=ZxOmMYC2Q_U4R7iFHOew@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Dec 04, 2011, mdf@freebsd.org wrote:
> On Wed, Nov 30, 2011 at 5:49 PM, David Schultz <das@freebsd.org> wrote:
> > Unfortunately, the "new type" is mostly useless, aside from
> > improving readability.  Unlike modern languages, C doesn't
> > consider it a compile-time error to mix up bools and ints.
> 
> Partly true.  In addition to changing the semantics of the ++ and --
> operators, _Bool is usually implemented as an 8-bit type and so
> pointers-to-bool and pointers-to-int are not compatible.  Also, C (or
> at least gcc? I don't know my C99 standard the way I know C89) will
> apply default promotion and casting rules, so that e.g. an assignment
> like:
> 
> int a;
> bool b;
> 
> a = 2;
> b = a;
> printf("%d", b);
> 
> will print 1.

What I meant by "mostly useless" is that programs like the above
don't elicit a compile-time error.  Static checks to prevent the
conflation of boolean and numeric types (unless you explicitly ask
for it) would promote correctness.  That's the most important
benefit of a boolean type, but with C99 you don't get that.
Kernighan even conceded as much in a paper advocating C over
Pascal.

The C99 bool type is still good for something because it documents
what the programmer meant, but the semantics are bizarre.  For
instance, gcc allows the statement 'return 2' in a boolean
function, and the result is that the function returns 1.

And incidentally, the "modern languages" I mentioned that get it
right include ALGOL 60!



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20111205181631.GA7393>