Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first syntax you use:</p> <pre><code> if (!someString) </code></pre> <p>exploits a sort of "ambiguity" of C deriving from the fact that the original standard of C lacked a proper boolean type. Therefore, any integer value equalling 0 was interpreted as "false", and any integer value different from "0" was taken as "true". The meaning of <code>!</code> is therefore defined based on this convention and current versions of the C standard have kept the original definition for compatibility.</p> <p>In your specific case, <code>someString</code> is a pointer, so it is first converted to an integer, then <code>! someString</code> is interpreted as a bool value of <code>true</code> when <code>someString</code> points at the location <code>0x000000</code>, otherwise it evals to "true".</p> <p>This is fine in most conditions (I would say always), but in theory, <code>NULL/nil</code> <a href="http://en.wikipedia.org/wiki/Pointer_(computer_programming)#Null_pointer" rel="nofollow noreferrer">could be different from <code>0x000000</code></a> under certain compilers, so (in very theory) it would be better to use the second syntax, which is more explicit:</p> <pre><code> if (someString == nil) </code></pre> <p>It is anyway more readable and since <code>someString</code> is not an integer (rather a pointer), IMO, better practice in general. </p> <p>EDIT: about the definition of NULL...</p> <p>Whether the C standard defines NULL to be 0 is an interesting topic for me...</p> <p>According to <a href="http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf" rel="nofollow noreferrer">C99 standard</a>, section 7.17, "Common definitions ":</p> <blockquote> <p>NULL [which] expands to an implementation-defined null pointer constant;</p> </blockquote> <p>So, NULL is defined in stddef.h to an <em>implementation-defined null pointer constant</em>... The same document on page 47 states:</p> <blockquote> <p>An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.</p> </blockquote> <p>So, the null pointer constant (which is <code>(void*)0</code>) can be <em>converted</em> to a null pointer and this is guaranteed to compare unequal to a pointer to any object or function. </p> <p>So, I think that basically it depends on whether the implementation decides that the result of converting a <em>null pointer constant</em> to a <em>null pointer</em> produces a pointer which converted back to an integer gives 0. It is not clear that a <em>null pointer</em> interpreted as an integer equals 0. </p> <p>I would say that the standard really try and enforce the null pointer being 0, but leaves the door open to <a href="https://stackoverflow.com/questions/2597142/when-was-the-null-macro-not-0">systems where the null pointer was not 0</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload