Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: question about nullptr Date: Wed, 10 Jul 2024 14:15:29 -0700 Organization: None to speak of Lines: 40 Message-ID: <87h6cxuexa.fsf@nosuchdomain.example.com> References: <20240706054641.175@kylheku.com> <877cdyuq0f.fsf@bsb.me.uk> <2ckiO.19403$7Ej.4487@fx46.iad> <87plrpt4du.fsf@bsb.me.uk> <9bCiO.7108$sXW9.3805@fx41.iad> <877cdwu9s1.fsf@nosuchdomain.example.com> <87y16bw1hf.fsf@nosuchdomain.example.com> <86r0c18gbl.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 10 Jul 2024 23:15:30 +0200 (CEST) Injection-Info: dont-email.me; posting-host="2ff96a67704e58776cf4f9c1dffac965"; logging-data="2150127"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Vq6Oij7jbMZ+D0augyYEs" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:+1F5aXZ+aO1MSGiEXzBglhGslvw= sha1:kvOq5OvFxvFYq8SBbOMLeFlqS7s= Bytes: 3276 Tim Rentsch writes: > Keith Thompson writes: >> Hmm. I like the idea of a type-agnostic way to express a "zero" >> value, [but] C's use of 0 for all scalar types strikes me more as >> an historical accident than a design feature. > > I don't think it was an accident at all. It was chosen to be > consistent with how if(), while(), !, ?:, and so forth, all act. > There is a very consistent design philosophy there. Sometimes > people who come from a strong Pascal background don't like it, > but personally I find the C model easier and more convenient to > work with than the Pascal model. In early C, int was in a very real sense the default type. In B, types weren't even explicit, and IIRC variables were effectively "of type int", or more precisely a 16-bit PDP-11 word. (I'm glossing over some details of B, many of which I don't know). In that context 0 made sense as a general-purpose "zero" value. Modern C has moved away from making int some kind of default (though it hasn't entirely done so). What I dislike is the fact that 0 is *both* of the specific type int *and* is commonly used as a general-purpose scalar 0. I accept it because it's the way the language is defined, but IMHO it's perhaps not an historical accident (you're right, it was deliberate), but more of an historical relic. Which is why I use 0 for integers, '\0' for characters, 0.0 for floating-point, and NULL (or nullptr if it's available) for pointers. If there were some general-purpose zero token that *isn't* of a specific type (I mentioned "{}" upthread), I'd probably use that. But it's not enough of a problem that I'd necessarily advocate a language change. And if someone else consistently uses 0 in pointer context, I'll waste about half a second being annoyed and then move on. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */