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

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
Safest way to copy a string?
One option is snprintf(dest,n,โ€œ%sโ€,src), but this will likely be a little slower due to the time needed to parse the format string. More on reddit.com
๐ŸŒ r/C_Programming
79
55
June 5, 2023
How do you add a null termination character at the end of a char array?
You literally just set that character to 0 (or '\0'), just the same way you would set any other value in the array. char foo[6]; foo[0] = 'H'; foo[1] = 'e'; foo[2] = 'l'; foo[3] = 'l'; foo[4] = 'o'; foo[5] = '\0'; More on reddit.com
๐ŸŒ r/C_Programming
14
0
May 24, 2023
How do I pass a string to a C function expecting char *?
[]u8 is a slice. It has a pointer and a length. You can use foo.ptr to grab the pointer part which is what you can send to C. Now ill give you a warning. []u8 is a pointer and a length. It is not really a c string. The difference being that a c string is strictly required to be null terminated. The correct type for a c string is ?[*:0]u8. Which is to say an optional (may be null) pointer to an array of unknown length which is terminated by a zero. You can ignore this in a lot of cases. However your type system isn't really enforcing the correct contracts if you aren't using the specific assumptions you need. This isn't all the information to this but it should be helpful. More on reddit.com
๐ŸŒ r/Zig
5
11
December 3, 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ strings-in-c
Strings in C - GeeksforGeeks
3 weeks ago - To find the length of a string in C, you need to iterate through each character until you reach the null terminator '\0', which marks the end of the string.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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
This means that every valid C string must end with a null-terminator so that functions and loops can recognize where the string finishes. Without this character, there is no way for C to know the boundary of the string, which can lead to serious problems.
๐ŸŒ
Quora
quora.com โ€บ Why-is-a-C-string-called-null-terminated-and-not-nul-terminated
Why is a C string called 'null terminated' and not 'nul terminated'? - Quora
Answer (1 of 6): The term null is generic, and independent of any particular character set. C defines the null character as a character whose integer value is zero. The mnemonic [code ]NUL[/code] is used by a number of character sets to name the null character in that character set. Thus, null ...
๐ŸŒ
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ passing-null-terminated-strings-to-c-libraries
Passing NULL-Terminated Strings to C Libraries | GeeksforGeeks
March 29, 2019 - First of it is that - it can be restricted to only operate on bytes using "y" conversion code to PyArg_ParseTuple() as shown in the code below. Code #2 : ... static PyObject * py_print_chars(PyObject * self, PyObject * args) { char * s; if (! PyArg_ParseTuple(args, "y", &s)) { return NULL; } print_chars(s); Py_RETURN_NONE; } Let's see the how to resulting function operates and how bytes with embedded NULL bytes and Unicode strings are rejected.
๐ŸŒ
YouTube
youtube.com โ€บ watch
044 Null terminated strings | Welcome to the course C programming - YouTube
| Welcome to the course C programming Book of C For This Course Drive - https://drive.google.com/file/d/1UBSeHDalbR6bdQU0MFQVa-hi8Sn9i0Xa/view?usp=sharing So...
Published ย  February 9, 2018
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-a-null-terminated-string-in-c-cplusplus
What is a null-terminated string in C/C++?
June 13, 2025 - #include <iostream> #include <cstring> #include <string> int main () { std::string str ("Please divide this sentance into parts"); char * cstr = new char [str.length()+1]; std::strcpy (cstr, str.c_str()); char * p = std::strtok (cstr," "); while (p!=0) { std::cout << p << '\n'; p = std::strtok(NULL," "); } delete[] cstr; return 0; }
๐ŸŒ
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.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ string โ€บ byte.html
Null-terminated byte strings - cppreference.com
February 21, 2023 - A null-terminated byte string (NTBS) is a possibly empty sequence of nonzero bytes followed by a byte with value zero (the terminating null character). Each byte in a byte string encodes one character of some character set.
๐ŸŒ
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 โ€บ display โ€บ c โ€บ STR31-C.+Guarantee+that+storage+for+strings+has+sufficient+space+for+character+data+and+the+null+terminator
STR31-C. Guarantee that storage for strings has sufficient space for character data and the null terminator - SEI CERT C Coding Standard - Confluence
In this compliant solution, characters are no longer copied to buf once index == BUFFERSIZE - 1, leaving room to null-terminate the string. The loop continues to read characters until the end of the line, the end of the file, or an error is encountered.
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ cobol-zos โ€บ 6.3.0
Manipulating null-terminated strings
You can construct and manipulate null-terminated strings (for example, strings that are passed to or from a C program) by various mechanisms.