Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What is the value after operator '!'? |
| Date | 2015-10-26 17:37 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87si4xjzui.fsf@bsb.me.uk> (permalink) |
| References | <7229ebda-7352-441e-9946-ccee1ce81b03@googlegroups.com> <87k2q9lp98.fsf@bsb.me.uk> <090eee37-7aeb-47e7-b195-ce3900b30fff@googlegroups.com> |
supercat@casperkitty.com writes: > On Monday, October 26, 2015 at 8:43:14 AM UTC-5, Ben Bacarisse wrote: >> From the C standard: >> >> "The result of the logical negation operator ! is 0 if the value of >> its operand compares unequal to 0, 1 if the value of its operand >> compares equal to 0." > > It may be worth noting that 0 doesn't always mean "zero". If p is a pointer, > then "p==0" will coerce the *literal* zero to a null pointer; I would not put it quite like that. 0 always means zero, it's just that an integer constant expression with the value 0 is a null pointer constant as well as being an integer zero. The 0 is not coerced to being a null pointer, it always is one. There *is* a conversion implicit in writing p==0, but it's the conversion of the null pointer constant to a null pointer of the same type as p. Your wording might be taken to suggest that there is some arithmetic type to pointer type conversion going on. > likewise !p > will compare p against a null pointer. It would be perfectly legitimate for > a system to define a mapping between integers and pointers such that: > > unsigned char *p = 0; // A literal zero -- a null pointer > unsigned char *q = (unsigned char*)(intptr_t)(1-1); // A zero value > > // The following two expressions both return 1 > > int resp1 = !p; // Since p is null) > int resq1 = !(intptr_t)q; // Since (intptr_t)q is zero > > // The following expressions could return 0 if a system uses a bit > // pattern for null which, when cast to intptr_t does not yield zero. > > int resp2 = !(intptr_t)p; // Comparing possibly-non-zero number vs zero > int resq2 = !q; // Compare possibly non-null pointer against null As for the very last part, I don't think so. Both p and q are null pointers of the same type. Whilst I agree that !(intptr_t)p might be 0, I can't see how can !q be anything other than 1. <snip> -- Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What is the value after operator '!'? fl <rxjwg98@gmail.com> - 2015-10-26 06:21 -0700
Re: What is the value after operator '!'? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-26 09:29 -0400
Re: What is the value after operator '!'? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 13:42 +0000
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 07:53 -0700
Re: What is the value after operator '!'? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 17:37 +0000
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 11:05 -0700
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 11:38 -0700
Re: What is the value after operator '!'? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-26 20:27 +0000
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 14:16 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 17:15 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 17:12 -0700
Re: What is the value after operator '!'? Ian Collins <ian-news@hotmail.com> - 2015-10-27 10:27 +1300
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 15:15 -0700
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 16:16 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-27 08:39 -0700
Re: What is the value after operator '!'? Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 14:04 +0000
Re: What is the value after operator '!'? Geoff <geoff@invalid.invalid> - 2015-10-26 09:50 -0700
csiph-web