Re: Pointers to "why C behaves like that ?"

"Nick Maclaren" <nmm1@cus.cam.ac.uk>
1 Dec 2002 22:35:11 -0500

          From comp.compilers

Related articles
[46 earlier articles]
Re: Pointers to "why C behaves like that ?" daniel_yokomiso@softhome.net (Daniel Yokomiso) (2002-11-26)
Re: Pointers to "why C behaves like that ?" thp@cs.ucr.edu (2002-11-26)
Re: Pointers to "why C behaves like that ?" vugluskr@unicorn.math.spbu.ru (Roman Shaposhnick) (2002-11-26)
Re: Pointers to "why C behaves like that ?" vugluskr@unicorn.math.spbu.ru (Roman Shaposhnick) (2002-11-26)
Re: Pointers to "why C behaves like that ?" vugluskr@unicorn.math.spbu.ru (Roman Shaposhnick) (2002-11-26)
Re: Pointers to "why C behaves like that ?" whopkins@alpha2.csd.uwm.edu (Mark) (2002-12-01)
Re: Pointers to "why C behaves like that ?" nmm1@cus.cam.ac.uk (Nick Maclaren) (2002-12-01)
Re: Pointers to "why C behaves like that ?" joachim_d@gmx.de (Joachim Durchholz) (2002-12-01)
Re: Pointers to "why C behaves like that ?" peter_flass@yahoo.com (Peter Flass) (2002-12-01)
Re: Pointers to "why C behaves like that ?" fjh@students.cs.mu.OZ.AU (Fergus Henderson) (2002-12-01)
Re: Pointers to "why C behaves like that ?" thp@cs.ucr.edu (2002-12-01)
Re: Pointers to "why C behaves like that ?" dot@dotat.at (Tony Finch) (2002-12-01)
Re: Pointers to "why C behaves like that ?" dot@dotat.at (Tony Finch) (2002-12-01)
[14 later articles]
| List of all articles for this month |
From: "Nick Maclaren" <nmm1@cus.cam.ac.uk>
Newsgroups: comp.compilers
Date: 1 Dec 2002 22:35:11 -0500
Organization: University of Cambridge, England
References: 02-11-059 02-11-071 02-11-122 02-11-154
Keywords: C, types
Posted-Date: 01 Dec 2002 22:35:11 EST

"jacob navia" <jacob@jacob.remcomp.fr> writes:
|> > If you mean that programs written using inferred types are less
|> > reliable than programs written in languages that require explicit
|> > declarations, I don't agree either. In my experience, the lack of
|> > distinction in C between booleans and integers ...[snip]
|>
|> C99 specifies the boolean type.


It specifies a type called _Bool, with properties that overlap
with those traditionally associated with the boolean type.


If you are into 'program proving' or even what we used to call the
practical end of that (rigorous validation), then the differences
start to matter. For example, let us assume that the compiler defines
__STDC_IEC_559__, there is an interesting little "gotcha":


        6.3.1.2 Boolean type


        [#1] When any scalar value is converted to _Bool, the result
        is 0 if the value compares equal to 0; otherwise, the result
        is 1.


        F.4 Floating to integer conversion


        [#1] If the floating value is infinite or NaN or if the
        integral part of the floating value exceeds the range of the
        integer type, then the ``invalid'' floating-point exception
        is raised and the resulting value is unspecified.


        F.8.3 Relational operators


        [#1]


        x != x -> false The statement x != x is true if x
                                                              is a NaN.


        x == x -> true The statement x == x is false if
                                                              x is a NaN.


Now consider


        _Bool weeble = NaN;


NaN compares unequal to 0 by F.8.3 and IEEE 754, so 6.3.1.2 states
that the result is 1. But F.4 says that the invalid exception is
raised and the value is unspecified. And I think that the similar
construction in Java specifies that no exception is raised and the
result is 0.




I suggest that any flame wars on this are taken to comp.std.c,
which I no longer look at; I am merely pointing out that there is
an issue relating to the detailed semantics, and it is NOT possible
to assume that C99's _Bool is a traditional boolean type.




Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1@cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.