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.

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.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-a-null-terminated-string-in-c-cplusplus
What is a null-terminated string in C/C++?
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 ...
Find elsewhere
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 181878-null-terminated-strings.html
null terminated strings
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.
๐ŸŒ
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:
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
The decision to make C strings null terminated with implied length instead of le... | Hacker News
March 8, 2021 - But also, "strings" and "time" are actually very complex concepts, and these functions operate on often outdated assumptions about those underlying abstractions ยท C99 came so very very close with VLAs. You can declare a function like:
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ Why-are-C-string-null-terminated
Why are C-string null terminated? - Quora
Answer (1 of 10): Bell Labs were a telecommunications company. Their designers thought in terms of telecommunications which were not reliable at transmitting data. This meant that transmissions needed to be framed with start and stop bit (a unique pattern that could not occur in data). The advan...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ c null terminated strings
Null Terminated Strings in C | Delft Stack
March 12, 2025 - Instead, it uses arrays of characters, and to define the end of a string, C uses a null character (\0). This character signals the termination of the string, allowing functions to determine where the string ends.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
SEI CERT
wiki.sei.cmu.edu โ€บ confluence โ€บ x โ€บ r9UxBQ
STR32-C. Do not pass a non-null-terminated character ...
The requested service is temporarily unavailable ยท We would like to apologize for any inconvenience that this may cause
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ string โ€บ multibyte.html
Null-terminated multibyte strings - cppreference.com
An NTMBS is only valid if it begins and ends in the initial shift state: if a shift sequence was used, the corresponding unshift sequence has to be present before the terminating null character. Examples of such encodings are BOCU-1 and SCSU. A multibyte character string is layout-compatible with null-terminated byte string (NTBS), that is, can be stored, copied, and examined using the same facilities, except for calculating the number of characters.
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Safe Coding Practices โ€“ Terminating a String | C For Dummies Blog
The array is still valid, and it could be used as an array of single char values, just not as a string. To ensure that youโ€™re working with a string and not just a collection of char variables waiting at a bus stop, cap the string with a null character, \0. This practice must be done any time ...