c++ - When is null character being used and what does it do? - Stack Overflow
Null character '\0' & null terminated strings
programming practices - Are C strings always null terminated, or does it depend on the platform? - Software Engineering Stack Exchange
c - How does one represent the empty char? - Stack Overflow
What is a null character?
How does the null character work in string termination?
How does the null character relate to the concept of 'null' in other programming contexts?
'\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]);
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!
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.
The things that are called "C strings" will be null-terminated on any platform. That's how the standard C library functions determine the end of a string.
Within the C language, there's nothing stopping you from having an array of characters that doesn't end in a null. However you will have to use some other method to avoid running off the end of a string.
Determination of the terminating character is up to the compiler for literals and the implementation of the standard library for strings in general. It isn't determined by the operating system.
The convention of NUL termination goes back to pre-standard C, and in 30+ years, I can't say I've run into an environment that does anything else. This behavior was codified in C89 and continues to be part of the C language standard (link is to a draft of C99):
- Section 6.4.5 sets the stage for
NUL-terminated strings by requiring that aNULbe appended to string literals. - 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, but there's also no reason to buck the established standard in most cases unless your goal is giving programmers fits. :-)
You can use c[i]= '\0' or simply c[i] = (char) 0.
The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.
You can't store "no character" in a character - it doesn't make sense.
As an alternative you could store a character that has a special meaning to you - e.g. null char '\0' - and treat this specially.
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.
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*.
String literals like "Hello World!" are null-terminated, but char arrays are not automatically null terminated.
The general principle I've always taken is to be extra cautious and assign '\0' to the the end of the string unless that causes a performance problem. In those cases, I'm extra careful about which library functions I use.
Always be careful to allocate enough memory with strings, compare the effects of the following lines of code:
char s1[3] = "abc";
char s2[4] = "abc";
char s3[] = "abc";
All three are considered legal lines of code (http://c-faq.com/ansi/nonstrings.htmlhttp://c-faq.com/ansi/nonstrings.html), but in the first case, there isn't enough memory for the fourth null-terminated character. s1 will not behave like a normal string, but s2 and s3 will. The compiler automatically count for s3, and you get four bytes of allocated memory. If you try to write
s1[3] = '\0';
that's undefined behavior and you're writing to memory that doesn't belong to s1, and would have weird effects, maybe even disrupting malloc's backend information, making it hard to free memory.