To your first question: I would go with Paul R's comment and terminate with '\0'. But the value 0 itself works also fine. A matter of taste. But don't use the MACRO NULLwhich is meant for pointers.

To your second question: If your string is not terminated with\0, it might still print the expected output because following your string is a non-printable character in your memory. This is a really nasty bug though, since it might blow up when you might not expect it. Always terminate a string with '\0'.

Answer from Lucas on Stack Overflow
๐ŸŒ
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.
Discussions

implementation - What are the advantages/disadvantages of null-terminated strings vs length-prefixed strings? - Programming Language Design and Implementation Stack Exchange
There are two main ways to implement strings in a language: null-terminated and length-prefixed strings. From Wikipedia: A null-terminated string is a character string stored as an array containin... More on langdev.stackexchange.com
๐ŸŒ langdev.stackexchange.com
Null terminated array of strings from C function
My code is calling the jack API. The function jack_get_ports is giving me a headache. The documentation says this function returns a null terminated array of ports (actually the portโ€™s names). This is the functionโ€™s signature generated by @cImport. pub extern fn jack_get_ports(client: ... More on ziggit.dev
๐ŸŒ ziggit.dev
1
0
November 12, 2023
Convenient null-terminated string literals - libs - Rust Internals
While Rust's native strings are better in general, there's a lot of existing C APIs that need null-terminated strings. It's good that CStr exists and it's what I'd turn to for dynamic strings. But it's pretty inconvenient if I need a static string literal, a case which was pretty common for ... More on internals.rust-lang.org
๐ŸŒ internals.rust-lang.org
0
February 10, 2021
Null character '\0' & null terminated strings
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. More on reddit.com
๐ŸŒ r/C_Programming
14
17
December 25, 2022

To your first question: I would go with Paul R's comment and terminate with '\0'. But the value 0 itself works also fine. A matter of taste. But don't use the MACRO NULLwhich is meant for pointers.

To your second question: If your string is not terminated with\0, it might still print the expected output because following your string is a non-printable character in your memory. This is a really nasty bug though, since it might blow up when you might not expect it. Always terminate a string with '\0'.

Answer from Lucas on Stack Overflow
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null-terminated_string
Null-terminated string - Wikipedia
March 25, 2025 - This allows the string to contain NUL and made finding the length need only one memory access (O(1) (constant) time), but limited string length to 255 characters. C designer Dennis Ritchie chose to follow the convention of null-termination to avoid the limitation on the length of a string and because maintaining the count seemed, in his experience, less convenient than using a terminator.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-a-null-terminated-string-in-c-cplusplus
What is a null-terminated string in C/C++?
June 13, 2025 - The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by '\0'). When we write some string using double quotes ("..."), then it is converted into null terminated strings by the compiler. The size of the string may smaller than ...
๐ŸŒ
Quora
quora.com โ€บ What-is-the-use-of-null-termination-in-the-C-programming-language
What is the use of null termination in the C programming language? - Quora
The presence of the null character, '\0โ€ฒ, signifies the end the string in the array. For example if I declared a char array of 8 chars long, I can use the strcpy function to store the string โ€œhelloโ€ in the char array.
Find elsewhere
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-how-to-ensure-string-null-termination-438491
How to ensure string null termination | LabEx
gcc -Wall -Wextra -Werror -O2 -g -fsanitize=address ## Enables comprehensive error checking ... Mastering string null termination is a fundamental skill in C programming.
Top answer
1 of 12
19

An option missing from the question is fat pointers โ”€ the type &str in Rust is an example of this. The length is not stored on the heap as a prefix to the string data, instead it is stored alongside the pointer, so that a reference to a string takes two words (length and pointer) instead of just one for a pointer.

This means that if there are multiple references to the same string, then the length data is duplicated compared to a length-prefixed string, which would only store the length once, where the string data is. But the upside is that a fat pointer can reference a substring without duplicating the string data on the heap.

In the diagram above (from the official Rust book), s is a String so it has a fat pointer to the whole string allocation (plus a capacity field, since it's a growable string), while world is a shared reference (i.e. a fat pointer) to a substring. This sharing would not be possible with length-prefixing, and would be possible with null-termination for substrings at the end of the string but not otherwise.

2 of 12
15

Length-prefixed strings have the advantage of being able to find their length in O(1) time rather than O(n) time. This means you can find the end of the string more easily with the length prefix. They are also less error prone to use since you don't have to deal with forgetting to null terminate a string.

One disadvantage to length prefixed strings is that they require more space. In addition, you are limited in what the max size of the string can be based on how many bytes are used to store the length.

๐ŸŒ
University of Kent
cs.kent.edu โ€บ ~durand โ€บ CS2 โ€บ Notes โ€บ 01_Intro โ€บ c2_stringCstyle.html
C-Style Strings
There are two ways to keep track of the the number of items in an array: ... A C-style string is a null (denoted by \0) terminated char array. The null occurs after the last character of the string. For an initialization using double quotes, "...", the compiler will insert the null.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 181878-null-terminated-strings.html
null terminated strings
January 1, 2024 - NOT "Null terminated!!! When a text line is read into memory, by fgets(), etc..., then the string, in memory, IS automatically Nul terminated by fgets(). Binary files of any file type, (Other than text files) are a whole different ballgame. You need to study a good up-to-date book on the C Programming Language.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_strings.htm
Strings in C
C provides a format specifier "%s" which is used to print a string when you're using functions like printf() or fprintf() functions. The "%s" specifier tells the function to iterate through the array, until it encounters the null terminator (\0) and printing each character.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-advantage-of-using-a-null-terminated-string-in-C-over-an-ordinary-string
What is the advantage of using a null-terminated string in C over an ordinary string? - Quora
Answer (1 of 2): The advantage is not crashing the program. C is very old, one of the oldest languages still in major production of modern programs. This is because it so useful and powerful and versatile. C strings are dumb things. They are just bytes in an array, nothing more. If those bytes ...
๐ŸŒ
ScienceDirect
sciencedirect.com โ€บ topics โ€บ computer-science โ€บ null-terminated-string
Null-Terminated String - an overview | ScienceDirect Topics
The two string representations described previously lead to radically different costs for the length computation. ... Null Terminated String The length computation must start at the beginning of the string and examine each character, in order, until it reaches the null character.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Null Terminated String Safety Issues | C Programming Tutorial - YouTube
An introduction to why null terminated strings are considered unsafe in C, including some common errors and solutions. Source code: https://github.com/portf...
Published ย  July 19, 2023
๐ŸŒ
Rust Internals
internals.rust-lang.org โ€บ libs
Convenient null-terminated string literals - libs - Rust Internals
February 10, 2021 - While Rust's native strings are better in general, there's a lot of existing C APIs that need null-terminated strings. It's good that CStr exists and it's what I'd turn to for dynamic strings. But it's pretty inconvenient if I need a static string literal, a case which was pretty common for ...
๐ŸŒ
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.
๐ŸŒ
Weber State University
icarus.cs.weber.edu โ€บ ~dab โ€บ cs1410 โ€บ textbook โ€บ 8.Strings โ€บ c_string.html
8.2. C-Strings
The null terminator can appear anywhere in the array, partially filling it if the terminator is not the last array element. The C-string functions ignore all array elements following the null terminator. The name of an array, without any trailing brackets, is the array's address. So, C++ often represents a C-string as a character pointer that points to an array.
๐ŸŒ
Ilya Safro
eecis.udel.edu โ€บ ~davis โ€บ cpeg222 โ€บ AssemblyTutorial โ€บ Chapter-20 โ€บ ass20_2.html
Null-terminated String
A null-terminated string is a sequence of ASCII characters, one to a byte, followed by a zero byte (a null byte). null-terminated strings are common in C and C++. Here is how a string is declared in assembly language: