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 - 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), Baudot, ITA2 codes, the C0 control code, and EBCDIC. In modern character sets, the null character ...
🌐
ASCII Code
ascii-code.com β€Ί character β€Ί ␀
Null character - ASCII Code
The null character is used in C and C++ as a string terminator, marking the end of a string of characters.
🌐
Wikipedia
en.wikipedia.org β€Ί wiki β€Ί Null-terminated_string
Null-terminated string - Wikipedia
March 25, 2025 - In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with an internal value of zero, called "NUL" in this article, not same as the glyph zero). Alternative names are C string, which ...
🌐
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.
🌐
OSDev Wiki
wiki.osdev.org β€Ί Null_Character
Null Character - OSDev Wiki
From this point forward, when we mention the null character/terminator, we will refer to a 8-bit ASCII NULL character, represented as 0x00 in hexadecimal or, in C, as the escape sequence '\0' (not to confuse with "\0", which in C is a null-terminated string containing one null character).
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.

🌐
Tutorial and Example
tutorialandexample.com β€Ί null-character-in-c
Null character in C - TAE
Null character in Cwith tutorial and examples on HTML, CSS, JavaScript, XHTML, Java, .Net, PHP, C, C++, Python, JSP, Spring, Bootstrap, jQuery, Interview Questions etc. - TAE
🌐
TutorialsPoint
tutorialspoint.com β€Ί ascii-nul-ascii-0-0-and-numeric-literal-0
ASCII NUL, ASCII 0 (β€˜0’) and Numeric literal 0?
Here we will see the ASCII NUL, ASCII 0 and the Numeric Literal 0. The ASCII null is represented as 0x00, and zero is represented as 0x30. The ASCII NUL character is used to denote the end of the string in C or C++. When programmer used β€˜0&r
Find elsewhere
🌐
PrepBytes
prepbytes.com β€Ί home β€Ί c programming β€Ί null character in c
Null Character in C
August 3, 2023 - The Null Character in C is a special character with an ASCII value of 0 (zero). It is not the same as the character β€˜0’ which has an ASCII value of 48. The Null Character in C is used to denote the end of a C string, indicating that there ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c++ β€Ί g-fact-72
ASCII NULL, ASCII 0 ('0') and Numeric literal 0 - GeeksforGeeks
July 23, 2025 - The ASCII (American Standard Code for Information Interchange) NULL and zero are represented as 0x00 and 0x30 respectively. An ASCII NULL character serves as a sentinel character of strings in C/C++. When the programmer uses '0' in his code, ...
🌐
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 - Null terminator: The null terminator ... the end of a file or stream. The null character is represented by the ASCII code 0 (NUL)....
🌐
Codedamn
codedamn.com β€Ί news β€Ί c programming
What is \ 0 (null byte) in C? Explained with examples.
November 9, 2023 - In the C language, \0 represents the null character. Not to be mistaken for the digit '0', it's a character with an ASCII value of zero. When you see \0 in code, you're looking at a single character that represents the number 0 in the ASCII table.
🌐
Javatpoint
javatpoint.com β€Ί null-character-in-c
Null character in C - javatpoint
This section will discuss the isalpha() function of the C programming language. The islpha() function is a predefined library function of the ctype.h header file that takes an argument as the character type and validates for valid alphabets.
🌐
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” ...
🌐
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 ...
🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί null character in c
r/learnprogramming on Reddit: NULL character in C
March 7, 2023 -

So, it's represented by '\0' or '0', right?

Why '0' too when it's value is 48 and not 0 - is it not the character '0' in ASCII but the symbol '0' used when talking about the NULL character?

EDIT: So it's just the ASCII character '\0'.

ALSO: Why is a char array printable as a string even if it contains no NULL character?

#include <stdio.h>

int main(void)
{
    char s[] = {'h', 'i', '.', '.'};
    printf("%s\n", s);
    printf("%i %i %i %i\n", s[0], s[1], s[2], s[3]);
}

prints

hi..
104 105 46 46

but if I change the last line to

printf("%i %i %i %i %i\n", s[0], s[1], s[2], s[3], s[4);

it complains "array index 4 is past the end of the array" which means there is no NULL.

EDIT: I just used the code above and changed s[] to s[5] and it printed the additional 0 - why - can't printf see the NULL if you declare the array without specifying its size in advance?

🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί difference-between-null-pointer-null-character-0-and-0-in-c-with-examples
Difference between NULL pointer, Null character ('\0') and '0' in C with Examples - GeeksforGeeks
July 15, 2025 - Each array is terminated with '\0' or null character but if we store a '0' inside a string both are not same according to the C language. '0' means 48 according to the ASCII Table whereas '\0' means 0 according to the ASCII table.