Null character

1 week ago 220

Representation

← Previous revision Revision as of 09:56, 11 July 2025
Line 36: Line 36:


== Representation ==
== Representation ==
Since the null character is not a [[printable character]] representing it requires special notation in [[source code]].
Since the null character is not a [[printable character]], representing it requires special notation in [[source code]].


In a [[string literal]], the null character is often represented as the [[escape sequence]] <code>\0</code> (for example, <code>"abc\0def"</code>). Similar notation is often used for a character literal (i.e. <code>'\0'</code>) although that is often equivalent to the numeric literal for zero (<code>0</code>).<ref name="KandR38">Kernighan and Ritchie, ''C'', p. 38: "The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0."</ref> In many languages ([[Escape sequences in C|such as C]], which introduced this notation), this is not a separate escape sequence, but an octal escape sequence with a single [[octal]] digit 0; as a consequence, <code>\0</code> must not be followed by any of the digits <code>0</code> through <code>7</code>; otherwise it is interpreted as the start of a longer octal escape sequence.<ref>In [[YAML]] this combination is a [http://www.yaml.org/spec/1.2/spec.html#id2776092 separate escape sequence].</ref> Other escape sequences that are found in use in various languages are <code>\000</code>, <code>\x00</code>, <code>\z</code>, or <code>\u0000</code>.
In a [[string literal]], the null character is often represented as the [[escape sequence]] <code>\0</code> (for example, <code>"abc\0def"</code>). Similar notation is often used for a character literal (i.e. <code>'\0'</code>) although that is often equivalent to the numeric literal for zero (<code>0</code>).<ref name="KandR38">Kernighan and Ritchie, ''C'', p. 38: "The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0."</ref> In many languages ([[Escape sequences in C|such as C]], which introduced this notation), this is not a separate escape sequence, but an octal escape sequence with a single [[octal]] digit 0; as a consequence, <code>\0</code> must not be followed by any of the digits <code>0</code> through <code>7</code>; otherwise it is interpreted as the start of a longer octal escape sequence.<ref>In [[YAML]] this combination is a [http://www.yaml.org/spec/1.2/spec.html#id2776092 separate escape sequence].</ref> Other escape sequences that are found in use in various languages are <code>\000</code>, <code>\x00</code>, <code>\z</code>, or <code>\u0000</code>.
Open Full Post