๐ŸŒ
ASCII Code
ascii-code.com โ€บ character โ€บ โ€
Null character - ASCII Code
Detailed information about ASCII character โ€, also known as the null character

control character whose bits are all 0

The null character is a control character with the value zero. Many character sets include a code point for a null character โ€“ including Unicode (Universal Coded Character Set), ASCII (ISO/IEC 646), โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null_character
Null character - Wikipedia
1 week ago - In a string literal, the null character is often represented as the escape sequence \0 (for example, "abc\0def"). Similar notation is often used for a character literal (i.e. '\0') although that is often equivalent to the numeric literal for zero (0).
Top answer
1 of 1
20

ASCII is only for [printable] character[s]

Not quite: in addition to printable characters, ASCII also includes a number of control characters.

ASCII code 0 (NUL) is one such control character.

To quote Wikipedia:

ASCII reserves the first 32 codes (numbers 0-31 decimal) for control characters: codes originally intended not to represent printable information, but rather to control devices (such as printers) that make use of ASCII, or to provide meta-information about data streams such as those stored on magnetic tape. For example, character 10 represents the "line feed" function (which causes a printer to advance its paper), and character 8 represents "backspace".

These days, the NUL character is most frequently used to signify the end of a character string in C. Its original purpose, however, was different:

The original meaning of this character was like NOP -- when sent to a printer or a terminal, it does nothing (some terminals, however, incorrectly display it as space). When electromechanical teleprinters were used as computer output devices, one or more null characters were sent at the end of each printed line to allow time for the mechanism to return to the first printing position on the next line. On punched tape, the character is represented with no holes at all, so a new unpunched tape is initially filled with null characters, and often text could be "inserted" at a reserved space of null characters by punching the new characters into the tape over the nulls.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ g-fact-72
ASCII NULL, ASCII 0 ('0') and Numeric literal 0 - GeeksforGeeks
July 23, 2025 - #include <stdio.h> int main() { char charNULL = '\0'; // Initialize a char variable with the null character unsigned int integer = 0; // Initialize an unsigned int variable with the value 0 char charBinary = '0'; // Initialize a char variable ...
๐ŸŒ
OSDev Wiki
wiki.osdev.org โ€บ Null_Character
Null Character - OSDev Wiki
In most of the dialects of assembly, strings variables (or more accurrately, labels) can be defined to be null terminated when created. Some assemblers accept the tag ".asciiz" followed by an string between double quotation marks to create (unsually in the data section) a string, that then you can treat as a normal C-like string. Example under 64 bit Intel assembly, with NASM:
Find elsewhere
๐ŸŒ
ASCII Table
ascii-code.com โ€บ 0
ASCII Code 0 - Null character
In the 7-bit ASCII character set, ASCII code 0 is represented by the control character โ€, also known as the null character.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ null character '\0' & null terminated strings
r/C_Programming on Reddit: Null character '\0' & null terminated strings
December 25, 2022 -

Hello everyone!
In C, strings (character arrays) are terminated by null character '\0' - character with value zero.
In ASCII, the NUL control code has value 0 (0x00). Now, if we were working in different character set (say the machine's character set wouldn't be ASCII but different one), should the strings be terminated by NUL in that character set, or by a character whose value is zero?

For example, if the machine's character set would be UTF-16, the in C, byte would be 16bits and strings would be terminated by \0 character with value 0x00 00, which is also NUL in UTF-16.
But, what if the machine's character set would be modified UTF-8 (or UTF-7, ...). Then, according to Wikipedia, the null character is encoded as two bytes 0xC0, 0x80. How would be strings terminated in that case? By the byte with value 0 or by the null character.

I guess my question could be rephrased as: Are null terminated strings terminated by the NUL character (which in that character set might be represented by a nonzero value) or by a character whose value is zero (which in that character set might not represent the NUL character).

Thank you all very much and I'm sorry for all mistakes and errors as english is not my first language.

Thanks again.

Top answer
1 of 3
31
should the strings be terminated by NUL in that character set, or by a character whose value is zero? The character '\0' is guaranteed to be a byte with all bits zero, and to have a numeric value equal to zero. A string in C always ends with this character. Then, according to Wikipedia, the null character is encoded as two bytes 0xC0, 0x80. No, in standard UTF-8 the code point with value zero is encoded in a single zero byte. You may have been reading something about "modified UTF-8", which appears to be a rather Java-centric external encoding for strings. It deliberately uses an "overlong" encoding of Java '\u0000' so that the resulting byte sequence does not contain a zero byte. One reason for this is because the length of strings in Java is not defined by use of a terminating character โ€” a Java string can contain arbitrary '\u0000' characters โ€” and you might need some way to round-trip such strings between Java and a language like C that does use a zero byte as a terminator.
2 of 3
17
C11 states: 5.2 Environmental considerations 5.2.1 Character sets 2. In a character constant or string literal, members of the execution character set shall be represented by corresponding members of the source character set or by escape sequences consisting of the backslash \ followed by one or more characters. A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string. Emphasis is mine From that we can understand that the terminating null character is always completely 0. Then, there's: 5.2.1.2 Multibyte characters A byte with all bits zero shall be interpreted as a null character independent of shift state. Such a byte shall not occur as part of any other multibyte character. 7.1.1 Definitions of terms A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.
๐ŸŒ
Quora
quora.com โ€บ What-is-a-null-character-NUL-on-a-system-level-and-how-can-we-use-it
What is a null character (NUL) on a system level, and how can we use it? - Quora
Answer: NUL is the ASCII character with a numeric value of zero. In C/C++ and close kin the nul character is used to mark the end of a string (rather than using the first byte or word of the โ€œstringโ€ to encode the number of characters in the string). There are some significant advantages to this...
๐ŸŒ
Medium
medium.com โ€บ @amey0x โ€บ null-byte-vs-null-terminator-vs-null-character-f25fc0ebc3f6
Null Byte vs Null terminator vs Null character ๐Ÿ˜ตโ€๐Ÿ’ซ | by Aman singh | Medium
October 27, 2023 - The null character is represented by the ASCII code 0 (NUL). It is also represented by the Unicode code point U+0000 (NULL). The null character is used in a variety of ways in computer programming. For example, it is used:
๐ŸŒ
Reddit
reddit.com โ€บ r/cs50 โ€บ why do i need to add null character to create a valid ascii text?
r/cs50 on Reddit: Why do I need to add null character to create a valid ASCII text?
March 5, 2021 -

My output looked correct however the check50 said output was not a valid ASCII text. After googling I fixed it by adding the null character to the end of the output array.

I understand that the null character is how C tells where the string ends, however I still don't fully understand why it was needed for my program, since the output that I see still looks the same whether or not I have that character. Since my output is in a char array so C already knows the size of that array.

Is someone able to explain why adding this character was needed? Is it just something that the check50 checks arbitrarily to qualify it as "valid ASCII text"?

Top answer
1 of 2
3

"NUL" is short for "NULL". Quoting ECMA-48, which lists all control characters as "8.3.xx acr - name":

8.3.88 NUL - NULL
Notation: (C0)
Representation: 00/00

NUL is used for media-fill or time-fill. NUL characters may be inserted into, or removed from, a data stream without affecting the information content of that stream, but such action may affect the information layout and/or the control of equipment.

Now, about your question:

In wikipedia it says that the ASCII NULL character is called NUL.

This is not what Wikipedia says. It says the ASCII null (lowercase!) character is called NUL. "Null character" is a term that applies to all character encodings, "NULL character" is not.

Wikipedia is correct, as there is nothing wrong with referring to this character by its official acronym.

In comment 2962254, Matteo Italia states that the NULL character is not called NULL.

He is wrong about that.

here, the table in "Character groups", the name is "NULL", abreviated "NUL"
here NUL is an abreviation, not a name

These are both correct.

in answer 30121329: ยซNUL is the name given to the first ASCII character.ยป

This is technically incorrect, but for all practical purposes correct. The acronyms are better known than the official names, and I don't see much of a problem in treating those acronyms as names.

2 of 2
1

Yes, nul, is an abbreviation of null.

Not a particularly shortened version as itโ€™s only shorter by one character, but as ASCII characters are limited to 3, so there isnโ€™t much choice really.

The uppercase standard of NUL is a convention of ASCII, for example, TAB.

๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ c programming
What is \ 0 (null byte) in C? Explained with examples.
November 9, 2023 - A frequent error made by new C programmers is not allocating memory for the null terminator. char word[5] = "Hello"; // This is incorrect. No space for '\0' While '0' is a character representing zero with ASCII value 48, '\0' is the null character ...
๐ŸŒ
ScienceDirect
sciencedirect.com โ€บ topics โ€บ computer-science โ€บ null-character
Null Character - an overview | ScienceDirect Topics
For example, the string โ€œHello Worldโ€ as a null-terminated string in ASCII is represented in hexadecimal as โ€œ48 65 6C 6C 6F 20 57 6F 62 6C 64 00โ€ and in decimal as โ€œ72 101 108 108 111 32 87 111 98 108 100 0โ€. 7 8 The binary representation for each character in โ€œHello Worldโ€ ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ scripting โ€บ zero null character and typing or inserting null in the terminal
Zero NULL Character and Typing or Inserting NULL in the Terminal | Baeldung on Linux
March 18, 2024 - In this case, we can see the first character is NULL, displayed as a combination of ^ caret and @ asperand. We use cat to get the ^@ caret notation of NULL in most examples, as the character would be invisible otherwise.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 730461 โ€บ java โ€บ null-null-character
null? null character? (Beginning Java forum at Coderanch)
If some reference is set to NULL this means: "This refernce does not 'point' to anything.". In addition to that: "text" often is refered to "readable" - which limits the range from "full" ASCII 0x00 - 0x7F to only 0x20 - 0x7E as the first 32 codepoints are the basic control characters which ...
๐ŸŒ
PrepBytes
prepbytes.com โ€บ home โ€บ c programming โ€บ null character in c
Null Character in C
August 3, 2023 - Ans: The Null character is represented in memory as a byte filled with all bits set to 0 (binary 00000000). It has an ASCII value of 0, and in C, it is typically represented using the escape sequence โ€˜\0โ€™.