🌐
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.
🌐
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 terminated string in C - Stack Overflow
Yes, you need to do that. Not all functions put the null char for you, and strncpy, as I can read in its man page, requires to have a null byte among the first n characters of src. More on stackoverflow.com
🌐 stackoverflow.com
Structure null character(\0) issue with strings in C programming Language - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams · Get early access and see previews of new features. Learn more about Labs ... Is the below issue happening because of null character. More on stackoverflow.com
🌐 stackoverflow.com
Advanced Mac Substitute is an API-level reimplementation of 1980s-era Mac OS
I made a MacOS system 7 web desktop UI with real web browsing: https://win9-5.com/macos/ · It's familiar and alien at the same time, like I'm seeing an alternate universe More on news.ycombinator.com
🌐 news.ycombinator.com
67
261
2 weeks ago
scanf - Is the null character a whitespace in C? - Stack Overflow
In the code below, isspace indicates that the null character is not a whitespace. Later in code the fwrite function writes a sequence of characters containing an intermediate and a final null chara... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is an invisible character?
An invisible character, often called a blank character or empty text, is a special Unicode character that appears as a blank space but is not technically a space. The most common one is the Hangul Filler, represented by the Unicode U+3164.Unlike a regular spacebar space, which is often trimmed or disallowed in usernames and form fields, these invisible characters are treated as actual characters by many systems.This allows you to:Create "invisible" or "blank" usernames on platforms like Discord, Fortnite, and Instagram.Send an apparently empty message on WhatsApp or Telegram.Center text or cre
🌐
quillbot.com
quillbot.com › tools › invisible-character
Invisible Character ( ) - Copy and Paste Blank Text
Can I type an invisible character with my keyboard?
For most users, doing so is not practical. It requires knowing the specific Alt code on Windows (e.g., ALT + 12644 for the Hangul Filler if you have the right registry settings) or using a character viewer on Mac. Our copy-paste tool is the fastest and most reliable method.
🌐
quillbot.com
quillbot.com › tools › invisible-character
Invisible Character ( ) - Copy and Paste Blank Text
Is an invisible character the same as a space?
No. A regular space is created by the spacebar (Unicode U+0020). An invisible character, like the Hangul Filler (U+3164), is a different character that just happens to have no visible glyph. Many websites are programmed to ignore leading/trailing spaces but will accept an invisible character.
🌐
quillbot.com
quillbot.com › tools › invisible-character
Invisible Character ( ) - Copy and Paste Blank Text
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
March 3, 2026 - A null-terminated string is a commonly used data structure in the C programming language, its many derivative languages and other programming contexts that uses a null character to indicate the end of a string. This design allows a string to be any length at the cost of only one extra character ...
🌐
QuillBot
quillbot.com › tools › invisible-character
Invisible Character ( ) - Copy and Paste Blank Text
If the copy button doesn't work, you can manually select the content from the text box below and copy it (using the copy icon or CTRL + C / CMD + C) ... Paste here to verify that the invisible character was copied successfully.
Find elsewhere
🌐
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.
🌐
Medium
medium.com › @maxnegi333 › what-is-a-null-character-in-c-programming-7d91a78f18ca
What is a Null character in C programming? - Mayanknegi - Medium
September 20, 2023 - In the C programming language, character strings are terminated with the Null character. To put it another way, the Null character in C stands for the end of a line, an array, or other concepts.
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.

🌐
Unityincommunity
unityincommunity.ca › 000-mah0-uneed-quickbox-m10-10
000 mah0 uneed quickbox m10 10: Why does a C null ...
info@unityincommunity.ca · Providing a space where youth feel safe and supported in every aspect of their lives—physically, emotionally, mentally, and culturally
🌐
Hacker News
news.ycombinator.com › item
Advanced Mac Substitute is an API-level reimplementation of 1980s-era Mac OS | Hacker News
2 weeks ago - I made a MacOS system 7 web desktop UI with real web browsing: https://win9-5.com/macos/ · It's familiar and alien at the same time, like I'm seeing an alternate universe
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › null-characters
Null Characters | Microsoft Learn
August 3, 2021 - Access to this page requires authorization. You can try changing directories. ... Any number of null characters can be appended to a binary stream.
Top answer
1 of 2
4

However, the fscanf at the end of the code only matches up to but not including the first null character.

That is incorrect, as is demonstrated by the fact that the output of the following program is “"Hello", then "world".” fscanf reads the entire line up to the new-line character; it does not stop at the null character.

#include <string.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char inBuf[40] = {0};
    char outBuf[] = "Hello\0world\n";
    FILE *fp = fopen("MyFile.txt", "w+b");
    fwrite(outBuf, 1, sizeof(outBuf), fp);
    fflush(fp);
    rewind(fp);
    fscanf(fp, "%s", inBuf);
    printf("\"%s\", then \"%s\".\n", inBuf, inBuf + strlen(inBuf) + 1);
}
2 of 2
2

the fscanf at the end of the code only matches up to but not including the first null character.

That is incorrect.

#include <stdio.h>

int main( void ) {
   char sp[]  = "abc def\n";
   char nul[] = "abc\0def\n";

   char buf1[10];
   char buf2[10];

   printf( "%d\n", sscanf( sp,  "%s %s", buf1, buf2 ) );
   printf( "%d\n", sscanf( nul, "%s %s", buf1, buf2 ) );
}
2   // First `%s` stopped at space, leaving characters for second %s.
1   // First `%s` stopped at LF, leaving nothing for second %s.

As you can see, it read to the end of the string rather than stopping at the NUL.

(You could also use ftell after your fscanf to get the number of bytes read.)

You did not indicate how you came to the conclusion that fscanf stopped at the NUL, but I presume you used something like printf( "%s\n", inBuf );. That stops at the first NUL. Not the reading.

🌐
Quora
quora.com › Do-I-have-put-null-character-in-C-style-string-whenever-I-declare-one-or-the-compiler-automatically-puts-null-characters
Do I have put null character in C style string whenever I declare one or the compiler automatically puts null characters? - Quora
Answer (1 of 6): Answered as: Do I have put null character in C style string whenever I declare one or the compiler automatically puts null characters? Sorry to sound a little preachy, but it's time for the OP to get access to a copy[1] of the “The C Programming Language, Second Edition” by Bria...
🌐
LabEx
labex.io › questions › what-is-the-purpose-of-null-in-a-c-string-array-136081
What is the purpose of NULL in a C string array? | LabEx
The purpose of NULL in a C string array is to mark the end of the string. In C, a string is not a built-in data type like in some other programming languages. Instead, a string is represented as an array of characters, where each character is ...
🌐
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).
🌐
Thecrankypress
thecrankypress.ca › offer › 000-gats-sandal-kulit-gh-003-tan
000 gats sandal kulit gh 003 tan: Why does a C null terminator ...
The Cranky Press is the hobby and the passion of its owner, Sue Globensky. (It can also be something of a love-hate relationship between a proprietor and her presses, but more on that elsewhere.) While the Press’s primary mandate is the entertainment of its proprietor, it serves also as a ...
🌐
cppreference.com
en.cppreference.com › cpp › string › basic_string
std::basic_string - cppreference.com
The class is dependent neither on the character type nor on the nature of operations on that type. The definitions of the operations are supplied via the Traits template parameter - a specialization of std::char_traits or a compatible traits class. The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [0, s.size()), and *(s.begin() + s.size()) has value CharT() (a null terminator)(since C++11); or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of an array(until C++11)a null-terminated array(since C++11) of CharT.