๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ difference-between-null-pointer-null-character-0-and-0-in-c-with-examples
Difference between NULL pointer, Null character ('\0') and '0' in C with Examples - GeeksforGeeks
July 15, 2025 - NULL is defined to compare equal to a null pointer as: ... if(!pointer) Null Characters('\0'): '\0' is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers.
Discussions

c++ - When is null character being used and what does it do? - Stack Overflow
The title almost says it all but: 1. I am reading a tutorial about character sequences and encountered a section that I do not really understand. They initialize an array of characters with a null More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
programming practices - Are C strings always null terminated, or does it depend on the platform? - Software Engineering Stack Exchange
Find your community ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Right now I am working with embedded systems and figuring out ways to implement strings on a microprocessor with no operating system. So far what I am doing is just using the idea of having NULL terminated character ... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
c - How does one represent the empty char? - Stack Overflow
The empty space char would be ' '. If you're looking for null that would be '\0'. ... Save this answer. ... Show activity on this post. Yes, c[i]='' is not a valid code. We parenthesis character constant between ' ', e.g. c[i] = 'A'; char A. More on stackoverflow.com
๐ŸŒ stackoverflow.com
People also ask

What is a null character?
In programming, a null character, often denoted as '0', is a special character with all its bits set to zero. It doesn't represent a visible character like 'A' or '1', but rather serves to terminate strings in languages like C. Essentially, it shows the end of a sequence of characters. This little zero-packed warrior is more of a behind-the-scenes hero, ensuring that your programs know where the strings end.
๐ŸŒ
lenovo.com
lenovo.com โ€บ home
What is a Null Character? A Comprehensive Explanation | Lenovo US
How does the null character work in string termination?
When you're working with strings in programming, the null character is like a silent sentinel. It's placed at the end of a sequence of characters to signal where the string concludes. This makes it easier for the program to know where the string's boundaries are. So, when your code meets a null character while processing a string, it understands that it has reached the end.
๐ŸŒ
lenovo.com
lenovo.com โ€บ home
What is a Null Character? A Comprehensive Explanation | Lenovo US
How does the null character relate to the concept of 'null' in other programming contexts?
While the null character ('0') is specific to strings and termination in languages like C, the concept of 'null' in other programming contexts generally refers to the absence of a value or a non-existent reference. It's crucial to differentiate between the null character used for string termination and the broader concept of 'null' or 'null values'in languages like Java, JavaScript, or structured query language (SQL).
๐ŸŒ
lenovo.com
lenovo.com โ€บ home
What is a Null Character? A Comprehensive Explanation | Lenovo US
control character whose bits are all 0
The null character is a control character with the value zero. Many character sets include a code point for a null character โ€“ including Unicode (Universal Coded Character Set), ASCII (ISO/IEC 646), โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null_character
Null character - Wikipedia
June 30, 2026 - The null character is a control character with the value zero. Many character sets include a code point for a null character โ€“ including Unicode (Universal Coded Character Set), ASCII (ISO/IEC 646), Baudot, ITA2 codes, the C0 control code, and EBCDIC. In modern character sets, the null character ...
๐ŸŒ
Lenovo
lenovo.com โ€บ home
What is a Null Character? A Comprehensive Explanation | Lenovo US
In programming, a null character, often denoted as '\0', is a special character with all its bits set to zero. It doesn't represent a visible character like 'A' or '1', but rather serves to terminate strings in languages like C. Essentially, it shows the end of a sequence of characters.
Top answer
1 of 2
4

'\0' is the null termination character. Without this you won't be able to know when the strings end. In your example

char foo[22] = { 'H', 'e', 'l', 'l', 'o' };

Is the same as

char foo[22] = { 'H', 'e', 'l', 'l', 'o', '\0', '\0', ..., '\0' };

With 17 times '\0'. When you Output the string you could have something like:

auto i = 0
do
{
    // Do smoething with foo[i++]
}
while('\0' != foo[i]);
2 of 2
1

When is the null character being used

Whenever a null terminated character string is needed. When do I need a null terminated character string, you might ask. Well, whenever you call a function that requires a null terminated character string. Whether a function requires a null terminated character string or not can be found from the documentation of the function. Examples of such functions are: std::strlen(const char*), operator<<(std::ostream&, const char*) and countless others.

when do you know if you have to input it explicitly

Whenever you need a null terminated character string and you either know that your character array would not otherwise be null terminated or you don't know whether it would be.

even though that the output of both examples is the exact same??

If you were to insert into an output stream a pointer to a character array that is not null terminated, the behaviour of the program would be undefined.

However, your character array contains nulls after the characters that you initialized explicitly. This is because value initialization sets characters without an explicit initializer to null.

What is it actually that the null character do?

It designates the end of a null terminated character string.


aren't all character sequences a null terminated character sequence then, since they all need that null character to indicate the end

If a character sequence ends in a null terminating character, then it is null terminated by definition. You can use such sequence as an argument to standard functions like std::strlen.

But no: A character sequence doesn't necessarily contain a null character. A character sequence that doesn't contain a null character is not null terminated by definition, and must not be used as an argument to functions that expect a null terminated string. An example:

char non_terminated[] = {'h', 'i', '!'};
auto len = std::strlen(non_terminated); // oops, UB!
Find elsewhere
๐ŸŒ
OSDev Wiki
wiki.osdev.org โ€บ Null_Character
Null Character - OSDev Wiki
The null character, also known as the null terminator, is a character with the value of zero. Besides representing a NOP, nowadays it is known as the control character that indicates the end of an string in C-like data formats. In essence, the null terminator is a way to encode the end of a ...
๐ŸŒ
ASCII Code
ascii-code.com โ€บ character โ€บ โ€
Null character - ASCII Code
ASCII CharactersASCII ArticlesASCII FAQASCII FactsASCII HistoryASCII GlossaryCompare Character SetsHTML SymbolHTML Color NamesHTTP status codes ... The null character (โ€) is a control character with the value zero. It is present in many character sets, including ASCII and its extensions, ...
๐ŸŒ
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.
๐ŸŒ
Facebook
facebook.com โ€บ groups โ€บ cs50 โ€บ posts โ€บ 2196690333811333
What's the difference between "null" and '\0'?
Find communities for youOver 1 billion people across the globe are using Facebook Groups to explore their favorite topics.
๐ŸŒ
Ecu
cs.ecu.edu โ€บ abrahamsonk โ€บ 2530 โ€บ fall18 โ€บ Notes โ€บ lec35.html
Null-Terminated Strings
A string constant such as "some text" is a null-terminated string. So it is an array of characters, with a null character at the end.
๐ŸŒ
University of Texas
farside.ph.utexas.edu โ€บ teaching โ€บ 329 โ€บ lectures โ€บ node21.html
Character strings
Here, 'f' represents the character ``f'', etc., and '\0' represents the so-called null character (ASCII code 0), which is used in C to signal the termination of a character string. The null character is automatically added to the end of any character string enclosed in double quotes.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-put-null-characters-into-a-string-in-the-C-programming-language
How to put null characters into a string in the C++ programming language - Quora
Answer (1 of 3): For C++, I suggest std::string, and letting it deal with null chars. IMO, a very bad idea to write null chars into std::strings If you must use C style strings, and sometimes you must use them, also donโ€™t write null the strings as a general best practice. Reasons you might have...
Top answer
1 of 4
12

The null character '\0' and the newline character '\n' are two different character values, just as 'x' and 'y' are two different character values.

The null character, whose value is 0, is used to mark the end of a string, which is defined by the C standard as "a contiguous sequence of characters terminated by and including the first null character." For example, the strlen() function, which returns the length of a string, works by scanning through the sequence of characters until it finds the terminating null character. (The length of a string doesn't count the terminating '\0'; strlen("foo") and strlen("foo\0") both yield 3.)

The newline character, '\n', is used to denote the end of a line in a text file. Strings exist in memory while your program is running, and lines exist in a text file external to your program. You can read the contents of a line (in a text file) into a string (in memory); depending on how you read it, the resulting string may or may not include the terminating '\n'. Null characters do not normally occur in text files.

Note carefully that NULL is (a macro that expands to) a null pointer constant. Other than the fact that both a null pointer and a null character can be expressed as 0, they have very little to do with each other. Please do not use the term NULL to refer to the null character.

One minor thing: in C, a character constant such as 'x', '\0', or '\n' is actually of type int, not of type char. (C++ differs in this.) But they're almost always used to denote values of type char. For example, this:

char c;
...
c = '\0';

will store a null character value in c, the int value is implicitly converted from int to char. In most cases, you don't have to worry about this.

char and int are both integer types, and you can freely convert between them. The reasons for character constants being of type int are historical.

Also, I see you're using old-style (K&R) function definitions. Way back in 1989, the ANSI standard added a new way to define functions using prototypes (you actually use some in your code) -- and there have been two new versions of the C standard since then. Old-style function definitions are obsolescent, and should be avoided. This:

int func(x, y)
int x;
char *y;
{
    /* ... */
}

is an old-style definition. This:

int func(int x, char *y)
{
    /* ... */
}

is a definition that uses a prototype, and it's preferred. For one thing, it lets the compiler check that a call passes the correct number and types of arguments.

You'll probably have more questions after this. I strongly suggest you take a look at the comp.lang.c FAQ; it will probably answer most of them.

2 of 4
3

Program #2 and #3 there are syntactical errors.

'\n' with Hex value 0x0a is often used to format text files o/p on screen just for readability.

'\0' with Hex value 0x00 is string delimiter. Although NULL has numeric value 0x0000 it's of type void*.

๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-tutorials-null-character
NULL Character in C - Sanfoundry
December 31, 2025 - These terms may look similar, but they serve different purposes in C: โ€˜0โ€™: A character that represents the digit zero. It has an ASCII value of 48. ... NULL: A macro that represents a null pointer, usually defined as ((void*)0).
Top answer
1 of 1
1

In c, a string is not an intrinsic type. A C-string is the convention to have a one-dimensional array of characters which is terminated by a null-character, by a '\0'.

Using that knowledge it should be

 struct date{  char day[3];  char month[3];  char year[5];  

Otherwise scanf1 won't be able to store the \0 within the array and it would be undefined behavior if you try to get 2 digit input (for day or month or 4 digit year by using %s format specifier) with it - because scanf will try to write it beyond the array and that would be Undefined Behaviour.

The scanf usage would be

if(scanf("%2s",current_date.day)!=1){
    fprintf(stderr,"Error in input\n");
    exit(EXIT_FAILURE);
}

The way you defined your structure - would give you 1 digit month, 1 digit day and 3 digit year if you store the corresponding \0 also. That is not what you want. In C strings are realized using nul terminated char array - scanf stores them. That's why in my case I have used %2s - so that the remaining space is there for storing \0.

1 Here note one thing from reference under %s format specifier

matches a sequence of non-whitespace characters (a string) If width specifier is used, matches up to width or until the first whitespace character, whichever appears first. Always stores a null character in addition to the characters matched (so the argument array must have room for at least width+1 characters).

Note: As pointed by Simon Berthiaume in comment -

You can write string length like this: char year[4+1];, that way it is clear what the content size is intended to be. For example in this case it is 4 digit year that you wanted.