Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The backslash notation in a character literal allows you to specify the numeric value of a character instead of using the character itself. So <code>'\1'</code>[*] means "the character whose numeric value is 1", <code>'\2'</code> means "the character whose numeric value is 2", etc. Almost. Due to a quirk of C, character literals actually have type <code>int</code>, and indeed <code>int</code> is used to handle characters in other contexts too, such as the return value of <code>fgetc</code>. So <code>'\1'</code> means "the numeric value as an int, of the character whose numeric value is 1", and so on.</p> <p>Since characters <em>are</em> numeric values in C, "the character whose numeric value is 1" actually <em>is</em> <code>(char)1</code>, and the extra decoration in <code>'\1'</code> has no effect on the compiler - <code>'\1'</code> has the same type and value as <code>1</code> in C. So the backslash notation is more needed in string literals than it is in character literals, for inserting non-printable characters that don't have their own escape code.</p> <p>Personally, I prefer to write <code>0</code> when I mean 0, and let implicit conversions do their thing. Some people find that very difficult to understand. When working with those people, it's helpful to write <code>'\0'</code> when you mean a character with value 0, that is to say in cases where you expect your <code>0</code> is soon going to implicitly convert to <code>char</code> type. Similarly, it can help to write <code>NULL</code> when you mean a null pointer constant, <code>0.0</code> when you mean a double with value 0, and so on.</p> <p>Whether it makes any difference to the compiler, and whether it needs a cast, depends on context. Since <code>'\0'</code> has exactly the same type and value as <code>0</code>, it needs to be cast to <code>char</code> in exactly the same circumstances. So <code>'\0'</code> and <code>(char)0</code> differ in type, for exactly equivalent expressions you can either consider <code>(char)'\0'</code> vs <code>(char)0</code>, or <code>'\0'</code> vs <code>0</code>. <code>NULL</code> has implementation-defined type -- sometimes it needs to be cast to a pointer type, since it may have integer type. <code>0.0</code> has type <code>double</code>, so is certainly different from <code>0</code>. Still, <code>float f = 1.0;</code> is identical to <code>float f = 1;</code> and <code>float f = 1.0f</code>, whereas <code>1.0 / i</code>, where <code>i</code> is an int, usually has a different value from <code>1 / i</code>.</p> <p>So, any general rule whether to use <code>'\0'</code> or <code>0</code> is purely for the convenience of readers of your code - it's all the same to the compiler. Pick whichever you (and your colleagues) prefer the look of, or perhaps define a macro <code>ASCII_NUL</code>.</p> <p>[*] or <code>'\01'</code> - since the backslash introduces an <em>octal</em> number, not decimal, it's sometimes wise to make this a bit more obvious by ensuring it starts with a 0. Makes no difference for 0, 1, 2 of course. I say "sometimes", because backslash can only be followed by 3 octal digits, so you can't write <code>\0101</code> instead of <code>\101</code>, to remind the reader that it's an octal value. It's all quite awkward, and leads to even more decoration: <code>\x41</code> for a capital A, and you could therefore write <code>'\x0'</code> for 0 if you want.</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.
    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