When/Why is '\0' necessary to mark end of an (char) array?
Asked Answered
T

5

16

So I just read an example of how to create an array of characters which represent a string.

The null-character \0 is put at the end of the array to mark the end of the array. Is this necessary?

If I created a char array:

char line[100]; 

and put the word:

"hello\n"

in it, the chars would be placed at the first six indexes line[0] - line[6], so the rest of the array would be filled with null characters anyway?

This books says, that it is a convention that, for example the string constant "hello\n" is put in a character array and terminated with \0.

Maybe I don't understand this topic to its full extent and would be glad for enlightenment.

Turtleback answered 26/11, 2016 at 18:4 Comment(6)
\0 is used to mark end of character string in C. Most C std library functions requires the string to be terminated this way in order to work.Spiral
Since C does not know how long is your string you must mark the end with a \0 so it knows it has reached the end of your string.Eolanda
You can use Pascal strings in C too. Of course, existing functions won't expect that so they won't work together.Varicocele
Possible duplicate of What's the rationale for null terminated strings?Cliffcliffes
Possible duplicate of Is it necessary to supply the null character when declaring an character's array?Edomite
When you refer to a book in a question, you should tell use what that book is (title, author, edition), so that any answer can be given context. Anyone with access to the book may be able to advise whether it is the book or your comprehension that is at fault. Certainly referring to this as a "convention" is somewhat confusing. String literals, such has "Hello" have a nul terminator - that is not a convention, that is defined in the language. Strings are however not a true data type in C as they are in some other languages and strings are stored in arrays.Senghor
B
5

When/Why is '\0' necessary to mark end of an (char) array?

The terminating zero is necessary if a character array contains a string. This allows to find the point where a string ends.

As for your example that as I think looks the following way

char line[100] = "hello\n";

then for starters the string literal has 7 characters. It is a string and includes the terminating zero. This string literal has type char[7]. You can imagine it like

char no_name[] = { 'h', 'e', 'l', 'l', 'o', '\n', '\0' };

When a string literal is used to initialize a character array then all its characters are used as initializers. So relative to the example the seven characters of the string literal are used to initialize first 7 elements of the array. All other elements of the array that were not initialized by the characters of the string literal will be initialized implicitly by zeroes.

If you want to determine how long is the string stored in a character array you can use the standard C function strlen declared in the header <string.h>. It returns the number of characters in an array before the terminating zero.

Consider the following example

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

int main(void) 
{
    char line[100] = "hello\n";
    
    printf( "The size of the array is %zu"
            "\nand the length of the stored string \n%s is %zu\n",
            sizeof( line ), line, strlen( line ) );
            
    return 0;
}

Its output is

The size of the array is 100
and the length of the stored string 
hello
 is 6

In C you may use a string literal to initialize a character array excluding the terminating zero of the string literal. For example

char line[6] = "hello\n";

In this case you may not say that the array contains a string because the sequence of symbols stored in the array does not have the terminating zero.

Beggar answered 26/11, 2016 at 18:39 Comment(0)
T
22

The \0 character does not mark the "end of the array". The \0 character marks the end of the string stored in a char array, if (and only if) that char array is intended to store a string.

A char array is just a char array. It stores independent integer values (char is just a small integer type). A char array does not have to end in \0. \0 has no special meaning in a char array. It is just a zero value.

But sometimes char arrays are used to store strings. A string is a sequence of characters terminated by \0. So, if you want to use your char array as a string you have to terminate your string with a \0.

So, the answer to the question about \0 being "necessary" depends on what you are storing in your char array. If you are storing a string, then you will have to terminate it with a \0. If you are storing something that is not a string, then \0 has no special meaning at all.

Trejo answered 26/11, 2016 at 18:12 Comment(0)
H
11

'\0' is not required if you are using it as character array. But if you use character array as string, you need to put '\0'.

There is no separate string type in C.

There are multiple ways to declare character array.

Ex:

char str1[]    = "my string";
char str2[64]  = "my string";
char str3[]    = {'m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0'};
char str4[64]  = {'m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g' };

All these arrays have the same string "my string". In str1, str2, and str4, the '\0' character is added automatically, but in str3, you need to explicitly add the '\0' character.

(When the size of an array is explicitly declared, and there are fewer items in the initializer list than the size of the array, the rest of the array is initialized with however many zeros it takes to fill it -- see C char array initialization: what happens if there are less characters in the string literal than the array size? and The N_ELEMENTS macro .).

Hoppe answered 26/11, 2016 at 18:10 Comment(0)
B
5

When/Why is '\0' necessary to mark end of an (char) array?

The terminating zero is necessary if a character array contains a string. This allows to find the point where a string ends.

As for your example that as I think looks the following way

char line[100] = "hello\n";

then for starters the string literal has 7 characters. It is a string and includes the terminating zero. This string literal has type char[7]. You can imagine it like

char no_name[] = { 'h', 'e', 'l', 'l', 'o', '\n', '\0' };

When a string literal is used to initialize a character array then all its characters are used as initializers. So relative to the example the seven characters of the string literal are used to initialize first 7 elements of the array. All other elements of the array that were not initialized by the characters of the string literal will be initialized implicitly by zeroes.

If you want to determine how long is the string stored in a character array you can use the standard C function strlen declared in the header <string.h>. It returns the number of characters in an array before the terminating zero.

Consider the following example

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

int main(void) 
{
    char line[100] = "hello\n";
    
    printf( "The size of the array is %zu"
            "\nand the length of the stored string \n%s is %zu\n",
            sizeof( line ), line, strlen( line ) );
            
    return 0;
}

Its output is

The size of the array is 100
and the length of the stored string 
hello
 is 6

In C you may use a string literal to initialize a character array excluding the terminating zero of the string literal. For example

char line[6] = "hello\n";

In this case you may not say that the array contains a string because the sequence of symbols stored in the array does not have the terminating zero.

Beggar answered 26/11, 2016 at 18:39 Comment(0)
I
4

You need the null character to mark the end of the string. C does not store any internal information about the length of the character array or the length of a string, and so the null character/byte \0 marks where it ends.

This is only required for strings, however – you can have any ordinary array of characters that does not represent a string.

For example, try this piece of code:

#include <stdio.h>

int main(void) {
    char string[1];
    string[0] = 'a';
    printf("%s", string);
}

Note that the character array is completely filled with data. Thus, there is no null byte to mark the end. Now, printf will keep printing until it hits a null byte – this will be somewhere past the end of the array, so you will print out a lot of junk in addition to just "a".

Now, try this:

#include <stdio.h>

int main(void) {
    char string[2];
    string[0] = 'a';
    string[1] = '\0';
    printf("%s", string);
}

It will only print "a", because the end of the string is explicitly marked.

Isaacson answered 26/11, 2016 at 18:14 Comment(0)
B
1

The length of a C string (an array containing the characters and terminated with a '\0' character) is found by searching for the (first) NUL byte. \0 is zero character. In C it is mostly used to indicate the termination of a character string. I make an example to you:

let's say you've written a word into a file:

word = malloc(sizeof(cahr) * 6);
word = "Hello";
fwrite(word, sizeof(char), 6, fp);

where in word we allocate space for the 5 character of "Hello" plus one more for its terminating '\0'. The fp is the file. An now, we write another word after the last one:

word2 = malloc(sizeof(cahr) * 7);
word2 = "world!";
fwrite(word2, sizeof(char), 7, fp);

So now, let's read the two words:

char buff = malloc(sizeof(char)*1000); // See that we can store as much space as we want, it won't change the final result
/* 13 = (5 chacater from 'Hello')+(1 character of the \0)+(6 characters from 'world!')+(1 character from the \0) */
fread(buff, sizeof(char), 13, fp); // We read the words 'Hello\0' and 'world!\0'
printf("the content of buff is: %s", buff); // This would print 'Hello world!'

This last is due to the ending \0 character, so C knows there are two separated strings into buffer. If we had not put that \0 character at the end of both words, and repeat the same example, the output would be "Helloworld!" This can be used for many string methods and functions!.

Briny answered 26/11, 2016 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.