Your line char *str = '\0'; actually DOES set str to (the equivalent of) NULL. This is because '\0' in C is an integer with value 0, which is a valid null pointer constant. It's extremely obfuscated though :-)

Making str (a pointer to) an empty string is done with str = ""; (or with str = "\0";, which will make str point to an array of two zero bytes).

Note: do not confuse your declaration with the statement in line 3 here

char *str;
/* ... allocate storage for str here ... */
*str = '\0'; /* Same as *str = 0; */

which does something entirely different: it sets the first character of the string that str points to to a zero byte, effectively making str point to the empty string.

Terminology nitpick: strings can't be set to NULL; a C string is an array of characters that has a NUL character somewhere. Without a NUL character, it's just an array of characters and must not be passed to functions expecting (pointers to) strings. Pointers, however, are the only objects in C that can be NULL. And don't confuse the NULL macro with the NUL character :-)

Answer from Jens on Stack Overflow
🌐
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

```

Discussions

c - How can I handle string if null character in the middle of string? - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I understand that string ends with a NULL. But if there is a null character(\0) in the middle of string, how can I handle the string? More on stackoverflow.com
🌐 stackoverflow.com
programming practices - Are C strings always null terminated, or does it depend on the platform? - Software Engineering Stack Exchange
Section 7.1.1 brings that to the functions in the standard library by defining a string as "a contiguous sequence of characters terminated by and including the first null character." There's no reason why someone couldn't write functions that handle strings terminated by some other character, ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
March 21, 2017
C - why can't we store the null character at the beginning of a string?
Sure you can. What makes you think you can't? By definition, C strings are terminated by a null character, so saying the null character is at the beginning of a string is just another way of saying the string is empty. More on reddit.com
🌐 r/learnprogramming
12
2
February 28, 2023
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
15
17
December 25, 2022
🌐
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
If the null-terminator is missing, these functions cannot operate safely, and the results are unpredictable. Always ensure that any character array meant to represent a string is properly null-terminated. This is fundamental for safe string ...
Top answer
1 of 4
8

You can't have a null character in the middle of a C string, because a null character, by definition, ends the string.

You can use arrays of chars where some of them are null characters, but you have to treat them as arrays, not strings. So you have to keep track of the length yourself.

2 of 4
5

string is ends with null character(\0), how can "e" be output?

The string literal "App\0le" is stored in memory as an unnamed character array having the following elements

char unnamed_string_literal[7] =  { 'A', 'p', 'p', '\0', 'l', 'e', '\0' };

This declaration

char *str = "App\0le";

may be rewritten taking into account the above assumption the following way

char *str = unnamed_string_literal;

So using the pointer arithmetic and knowing a priori the number of elements in the string literal (including its embedded zero character) you can output any elements of the character array that represents the string literal.

For example

#included <stdio.h>

int main( void )
{
    char *str = "App\0le";

    for (size_t i = 0; i < 7; i++)
    {
        if (str[i] == '\0')
        {
            putchar( '\\' ), putchar( '0' );
        }
        else
        {
            putchar( str[i] );
        }
    }

    putchar( '\n' );
}

The program output is

App\0le\0

That is the expression str[i] is an expression of accessing i-th element of an array. It is totally unimportant what the type of the array and what it stores.

If you will write

char *str2 = str;

then the pointer str2 will point to the first character of the same string literal pointed to by the pointer str.

If you need to get a string then you need to declare a character array as for example

char str2[6];

and copy to it characters of the string literal pointed to by the pointer str excluding the embedded zero character but including the terminating zero character. You may not change the string literal itself because any attempt to change a string literal results in undefined behavior.

For example (without using standard C string functions)

#include <stdio.h>

int main( void )
{
    char *str = "App\0le";
    char str2[6];

    size_t i = 0;

    while (( str2[i] = str[i] ) != '\0') i++;
    while (( str2[i] = str[i + 1] ) != '\0') i++;

    puts( str2 );
}

The program output is

Apple
🌐
Sanfoundry
sanfoundry.com › c-tutorials-null-character
NULL Character in C with Examples
December 31, 2025 - This program creates a string called “Bootcamp”. It uses strlen() to find the length of the string, which stops at the null character ‘\0‘. The length is stored in the size_t variable len, and then the program prints the length using printf().
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 58206-setting-string-null.html
Setting a string to null
October 24, 2004 - Or do you have a string and you want to make it an empty string? >strcpy(string, NULL); This isn't a good idea, strcpy expects both arguments to be non-null pointers to C-style strings. >*string=NULL; This isn't a good idea either, NULL should only be used in pointer context.
Find elsewhere
🌐
PrepBytes
prepbytes.com › home › c programming › null character in c
Null Character in C
August 3, 2023 - Therefore, if an input contains a Null Character in C, it will be considered the end of the input, truncating the string at that point. This behavior should be considered when designing programs that handle user input.
🌐
C For Dummies
c-for-dummies.com › blog
Null Versus Empty Strings | C For Dummies Blog
August 12, 2017 - A null string has no values. It’s an empty char array, one that hasn’t been assigned any elements. The string exists in memory, so it’s not a NULL pointer.
🌐
LabEx
labex.io › tutorials › c-how-to-ensure-string-null-termination-438491
How to ensure string null termination | LabEx
Mastering string null termination is a fundamental skill in C programming. By implementing careful allocation, copying, and validation techniques, developers can create more reliable and secure string-handling code, minimizing the risk of buffer overflows and unexpected program behavior.
🌐
Quora
quora.com › What-is-a-null-string-in-C
What is a null string in C? - Quora
Answer (1 of 2): The term “null string” is very ambiguous in a language like C. It might mean that you have a pointer to char which is set to NULL. In this case, there is no string at all. All you have is a pointer to NULL (a pointer that contains memory address 0). To create one of these, ...
🌐
Delft Stack
delftstack.com › home › howto › c null terminated strings
Null Terminated Strings in C | Delft Stack
March 12, 2025 - In C, a string is essentially an array of characters. However, unlike other languages, C does not have a built-in string data type. Instead, it uses arrays of characters, and to define the end of a string, C uses a null character (\0).
🌐
How To
blog.squidvision.com › home › foolproof tips for null string detection in c
Foolproof Tips for Null String Detection in C
September 22, 2024 - The significance of strcmp() in this context lies in its ability to establish the equality of two strings. By comparing a string to a null string, programmers can effectively determine if the string is empty or contains any characters. This information is crucial for various string manipulation tasks and error handling scenarios.
🌐
Wikipedia
en.wikipedia.org › wiki › Null-terminated_string
Null-terminated string - Wikipedia
March 25, 2025 - C designer Dennis Ritchie chose ... 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. This had some influence on CPU instruction set design. Some CPUs in the 1970s and 1980s, such as the Zilog Z80 and the DEC VAX, had dedicated instructions for handling length-prefixed ...
🌐
Northern Illinois University
faculty.cs.niu.edu › ~winans › CS501 › Notes › cstrings.html
C Strings
The individual characters that make up the string are stored in the elements of the array. The string is terminated by a null character. Array elements after the null character are not part of the string, and their contents are irrelevant. A "null string" or "empty string" is a string with ...
🌐
Tutorial and Example
tutorialandexample.com › null-character-in-c
Null character in C - TAE
March 28, 2022 - Null character in Cwith tutorial and examples on HTML, CSS, JavaScript, XHTML, Java, .Net, PHP, C, C++, Python, JSP, Spring, Bootstrap, jQuery, Interview Questions etc. - TAE
🌐
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.