Strings in C and related programming languages are terminated with a 'NUL' character (ascii 0).

An idea which has at least a couple of shortcomings. You have to traverse the entire string to find the length, and if you (or your code) forgets the add the NULL to the end, you're screwed.

An idea which has great advantages, however, over the Pascalish length byte. This sets arbitrary limits on the length of the string (generally 255) and forces you to always update the length byte when manipulating the string.

I'd just like to point out that NULL is technically a pointer, not an
integer constant, although you can technically put a NULL character at the
end (since NULL == (void *)0x0, which is 0 when typecast to an int or
char). However, in the context of character constants, it's usually spelled
'null,' not 'NULL.' Also, there's no such thing as NUL in C (unless you're
referring to the ASCII name for it).

The NULL Pointer is by definition not required to have the value 0, but is merely an always "invalid" pointer. A C string is not null terminated - it is zero terminated. The correct way is st\len\ = '\0' or st\len\ = (char)0. Though other methods of assigning a zero value to the last byte may work, thay are not necessarily correct.

NUL is a character, NULL is a pointer. You can NULL-terminate any array of pointers, or you can NUL-terminate a string (or even put the null character at the end). Just don't mix the two up (and certainly not on certain Usenet newsgroups).

According to the ANSI base document, The Annotated C++ Reference Manual, by Bjarne Stroustrup and Margaret Ellis,
    The following conversions may be performed wherever pointers are assigned, initialized, compared, or otherwise used: A constant expression that evaluates to zero is converted to a pointer, commonly called the null pointer. It is guaranteed that this value will produce a pointer distinguishable from a pointer to any object or function. {...}

That is, in C++, any zero integer is treated as the null pointer, if the expression is used in any context where a pointer is required. While it's theoretically possible to make a CPU that uses 0xFFFFFFFF as the 'null pointer' value, or even some bizarre value like 0xDEADBEEF, in practice, all-zero is NULL.

ANSI C++ compliant header files often define NULL this way:

    #ifndef NULL
    #ifdef __cplusplus
    #define NULL    0
    #define NULL    ((void *)0)

This highlights a difference:

  • in C, NULL is defined as ((void*)0)
  • in C++, NULL is defined as 0
  • Thus, in C++, you can say char ch = NULL; and not be ruinously gauche.

    Strictly speaking, "NULL terminator" (the title of this node), can refer to a number of things:

  • At the ends of a linked list chain, the head and tail nodes often use NULL terminators to halt further traversal.
  • At the end of a table of structured data, a recognizable terminator may be used. Often, the last static initializer uses NULL pointers or other zeroes. This is a NULL terminator.
  • At the end of a string of bytes, intended to be used as a textual string, the NUL ASCII character '\0' is appended. This is done automatically by the C or C++ compiler at the end of any double-quoted string token. Whether spelled 'NUL' or 'NULL', this is a NULL terminator.
  • At the end of a SCSI bus, a terminator circuit must be placed somewhere on the bus to avoid signal echoing. Some call this a NULL terminator, as it is attached to the bus like a SCSI device, but uses no SCSI device identifier.
  • Log in or registerto write something here or to contact authors.