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
🌐
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.

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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 the array size, but if there are some null character inside that array, that will be treated as the end of that string. Here, we assign null character in the 13th index position, since indexing start from 0 of the string to the null character (\0).
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_strings.htm
Strings in C
In the following example, you can input a string using scanf() function, after inputting the specific characters (5 in the following example), we are assigning null ('\0') to terminate the string.
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › null-terminated-string
Null-Terminated String - an overview | ScienceDirect Topics
The following simple loop will move the characters on a machine with byte-oriented cload and cstore operations: Notice that this code tests the lengths of a and b to avoid overrunning a. (With an explicit length representation, the overhead is small.) The label Lsov represents a runtime error handler for string-overflow conditions. In c, which uses null termination ...
🌐
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.
🌐
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
Answer (1 of 5): In C character strings are stored in char arrays. 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.
🌐
Ilya Safro
eecis.udel.edu › ~davis › cpeg222 › AssemblyTutorial › Chapter-20 › ass20_2.html
Null-terminated String
In the red text, space (0x20) is shown as '_'. The null byte is at address 0x1000001B.
🌐
Weber State University
icarus.cs.weber.edu › ~dab › cs1410 › textbook › 8.Strings › c_string.html
8.2. C-Strings
String constants or string literals, like "hello world", are also C-strings. When the compiler processes a string literal, it adds the null termination character at the end of the quoted characters, stores them in memory, and generates code based on the string's address.
🌐
C For Dummies
c-for-dummies.com › blog
Safe Coding Practices – Terminating a String | C For Dummies Blog
For example, you might be lucky ... 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....
🌐
University of Kent
cs.kent.edu › ~durand › CS2 › Notes › 01_Intro › c2_stringCstyle.html
C-Style Strings
The resulting string includes a null-character at end. Parameters. ... Pointer to a null-terminated string with enough space allocated to contain src plus num characters.
🌐
Reddit
reddit.com › r/c_programming › should i end a dynamically allocated array with a null terminated character?
r/C_Programming on Reddit: Should I end a dynamically allocated array with a null terminated character?
January 13, 2023 -

I am confused with this topic, my teacher said that strings in C are null terminated automatically then if I manually allocate 5 bytes for the string "word", should I add "\0" at the end or not?

Thank you for answering my question!

Top answer
1 of 4
5
Your teacher was probably talking about string literals. That is, char s[] = "word"; printf("%zu\n", sizeof s); prints 5, and s can be passed to all string-related functions and work exactly as expected. However, this is because of the initialisation line. If you dynamically allocate the string instead, you'll need to remeber to add the null terminator.
2 of 4
3
First off, it's good that you're thinking about this. I suspect that your teacher's statement might be some Obi-Wan "true, from a certain point of view" speak. As both u/balkenbrij and u/Ninesquared81 pointed out, if you have a string literal in your code, when compiled, a null byte will be added, and in that sense, the string is "null terminated automatically." If you are using the string APIs in the C standard library, they will expect their inputs to be null-terminated, and will null-terminate their outputs, and in that sense as well, the string is "null terminated automatically." if I manually allocate 5 bytes for the string "word", should I add "\0" at the end or not? Short answer, yes. A string in C has two characteristics: it's a block of contiguous memory, and it has a zero byte at the end. Allocating the memory gets you the first, but if you're not using a string API you need to do the second. You can either manually write a \0 after the end of the character sequence, or just use calloc to get a zero-initialized memory block and then it's null-terminated by default*. Long answer: if you are allocating a char[] manually, and it is for a string in the C sense, and you expect to be able to use that manually-allocated character array as a string in the string APIs, then you must null-terminate it yourself. The reason I add this caveat is that, in general, an arbitrary data structure does not have to be null-terminated: if I have a fixed-size structure, then there's no reason to add a terminator to indicate the end, I already know how big it is. I'm not terribly well-versed on the history of the C language, but I suspect this is an accident of the early days: by using a null-terminator to mark the end of the string, you can avoid having to maintain a struct with both the char pointer and a length element, which could save potentially several bytes per string. Several bytes! * when using calloc, your string is null-terminated by default unless you overflow the allocated region, then all bets are off. Any time you are manually manipulating memory, be alert to the potential for overflows.