String literals like "Hello World!" are null-terminated, but char arrays are not automatically null terminated.

The general principle I've always taken is to be extra cautious and assign '\0' to the the end of the string unless that causes a performance problem. In those cases, I'm extra careful about which library functions I use.

Answer from Michael Price on Stack Overflow
🌐
University of Texas
farside.ph.utexas.edu › teaching › 329 › lectures › node21.html
Character strings
The null character is automatically added to the end of any character string enclosed in double quotes. Note that, since all character strings in C must be terminated by the (invisible) null character, it takes a character array of size at least n+1 to store an n-letter string.
🌐
Sanfoundry
sanfoundry.com › c-tutorials-null-character
NULL Character in C with Examples
December 31, 2025 - These terms may look similar, but they serve different purposes in C: ‘0’: A character that represents the digit zero. It has an ASCII value of 48. ... NULL: A macro that represents a null pointer, usually defined as ((void*)0). ‘\0’: ...
🌐
Tutorial and Example
tutorialandexample.com › null-character-in-c
Null character in C - TAE
March 28, 2022 - The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string or end of an array or other concepts in C. The end of the character string or the NULL byte is represented by ‘0’ or ...
🌐
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 4
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 4
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.
🌐
PrepBytes
prepbytes.com › home › c programming › null character in c
Null Character in C
August 3, 2023 - 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 are no more characters in the sequence after it.
🌐
Wikipedia
en.wikipedia.org › wiki › Null_character
Null character - Wikipedia
June 30, 2026 - 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. A null-terminated string is a ...
Find elsewhere
🌐
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).
🌐
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 - It stores each of the characters in a memory space of 1 byte. 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.
🌐
OSDev Wiki
wiki.osdev.org › Null_Character
Null Character - OSDev Wiki
The null character, also known as the null terminator, is a character with the value of zero. Besides representing a NOP, nowadays it is known as the control character that indicates the end of an string in C-like data formats. In essence, the null terminator is a way to encode the end of a ...
🌐
SoftHandTech
softhandtech.com › what-is-null-character-in-c
Understanding the Null Character in C: A Comprehensive Guide - SoftHandTech
5 days ago - A null character, denoted by \0, is a special character in C that represents the end of a string. It is an ASCII character with a value of 0, which distinguishes it from other characters. The null character is not visible when printed, and its primary purpose is to serve as a sentinel value, ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_strings.htm
Strings in C
A string in C is a one-dimensional array of char type, with the last character in the array being a "null character" represented by '\0'. Thus, a string in C can be defined as a null-terminated sequence of char type values.
🌐
Medium
medium.com › @maxnegi333 › what-is-a-null-character-in-c-programming-7d91a78f18ca
What is a Null character in C programming? - Mayanknegi - Medium
September 20, 2023 - In the C programming language, character strings are terminated with the Null character. To put it another way, the Null character in C stands for the end of a line, an array, or other concepts.
Top answer
1 of 3
3

Does that mean that according to the standard it is legal to use the NULL constant as the terminator? (OP)

str1[3] = NULL;

Sometimes. Further: does it always properly cause a character array to form a string without concerns?

First, it looks wrong. Akin to int z = 0.0;. Yes it is legal well defined code, but unnecessarily draws attention to itself.

In practice, I have always used NULL instead of '\0' (OP)

I doubt you will find any modern style guide or group of coders endorsing that. NULL is best reserved for pointer contexts.1

These are 2 common and well understood alternatives.

str1[3] = '\0';
str1[3] = 0;

strings in C are null-terminated (OP)

The C spec consistently uses null character, not just null.


The macros are NULL which expands to an implementation-defined null pointer constant; and ... C11 §7.19 3

OK, now what is a null pointer constant?

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. §6.3.2.3 5

If the null pointer constant is a void* then we have something like

str1[3] = (void*) 0;

The above can warn about converting a pointer to a char. This is something best avoided.


Will it be the same in case of C++? (OP)

Yes, the above applies. (Aside: str1[3] = 0 may warn.) Further, NULL is less preferred than nullptr. So NULL is rarely the best to use in C++ in any context.


1Note: @Joshua reports a style that matches OP's in 1995 Turbo C 4.5

2 of 3
3

The bottom line is that in C/C++, NULL is for pointers and is not the same as the null character, despite the fact that both are defined as zero. You might use NULL as the null character and get away with it depending on the context and platform, but to be correct, use '\0'. This is described in both standards:

  • C specifies that the macro NULL is defined as a macro in <stddef.h> which "expands to an implementation-defined null pointer constant" (Section 7.17.3), which is itself defined as "an integer constant expression with the value 0, or such an expression cast to type void *" (Section 6.3.2.3.3).

    The null character is defined in section 5.2.1.2: "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." That same section explains that \0 will be the representation of this null character.

  • C++ makes the same distinctions. From section 4.10.1 of the C++ standard: "A null pointer constant is an integer literal (2.13.2) with value zero or a prvalue of type std::nullptr_t." In section 2.3.3, it describes the as "null character (respectively, null wide character), whose value is 0". Section C.5.2 further confirms that C++ respects NULL as a standard macro imported from the C Standard Library.

🌐
Reddit
reddit.com › r/c_programming › how do i print out the null value in a string?
r/C_Programming on Reddit: How do I print out the null value in a string?
December 4, 2023 -

I learned in C that a string ends with a null value, "\0". How do I print out this null value in C?

I tried doing this by scanning the string "paint". However, it doesn't seem to work -

```
#include <stdio.h>
int main() {
char name[100];
scanf("%s", name);
printf("The name is %c", name[5]);
}

```

This is my output -
```
paint

The name is some weird symbol looking like 0

Process finished with exit code 0

```

🌐
Lenovo
lenovo.com › home
What is a Null Character? A Comprehensive Explanation | Lenovo US
In programming, a null character, often denoted as '\0', is a special character with all its bits set to zero. It doesn't represent a visible character like 'A' or '1', but rather serves to terminate strings in languages like C. Essentially, it shows the end of a sequence of characters.
🌐
Codefinity
codefinity.com › courses › v2 › 5ce35233-a099-4fe9-b172-f52fe1e84d86 › 53e2c5b5-e032-49cc-877b-e4654728148f › 0eb5d9e1-f7f4-4f74-a46d-a365cab96757
Learn Null-Termination and Its Importance | String Representation in C
However, unlike some other languages, C does not store the length of a string alongside the data. Instead, C uses a special character called the null-terminator—represented as \0—to mark the end of a string.