Why do we need to add a '\\0' (null) at th

2019-01-14 13:35发布

问题:

Why do we need to add a '\0' (null) at the end of a character array in C? I've read it in K&R 2 (1.9 Character Array). The code in the book to find the longest string is as follows :

#include <stdio.h>
#define MAXLINE 1000
int readline(char line[], int maxline);
void copy(char to[], char from[]);

main() {
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];
    max = 0;
    while ((len = readline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0)
        printf("%s", longest);
    return 0;
}

int readline(char s[],int lim) {
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0'; //WHY DO WE DO THIS???
    return i;
}

void copy(char to[], char from[]) {
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

My Question is why do we set the last element of the character array as '\0'? The program works fine without it... Please help me...

回答1:

You need to end C strings with '\0' since this is how the library knows where the string ends (and, in your case, this is what the copy() function expects).

The program works fine without it...

Without it, your program has undefined behaviour. If the program happens to do what you expect it to do, you are just lucky (or, rather, unlucky since in the real world the undefined behaviour will choose to manifest itself in the most inconvenient circumstances).



回答2:

In c "string" means a null terminated array of characters. Compare this with a pascal string which means at most 255 charactes preceeded by a byte indicating the length of the string (but requiring no termination).

Each appraoch has it's pros and cons.



回答3:

Especially string pointers pointed to array of characters without length known is the only way NULL terminator will determine the length of the string.

Awesome discussion about NULL termination at link



回答4:

Because C defines a string as contiguous sequence of characters terminated by and including the first null character.

Basically the authors of C had the choice to define a string as a sequence of characters + the length of string or to use a magic marker to delimit the end of the string.

For more information on the subject I suggest to read this article:

"The Most Expensive One-byte Mistake" by Poul-Henning Kamp http://queue.acm.org/detail.cfm?id=2010365



回答5:

You have actually written the answer yourself right here:

void copy(char to[], char from[]) {
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

The loop in this function will continue until it encounters a '\0' in the array from. Without a terminating zero the loop will continure an unknown number of steps, until it encounters a zero or an invalid memory region.



回答6:

Really, you do not need to end a character array by \0. It is the char*, or the C representation of the string that needs to be ended by it.

As for array, you have to add a \0 after its end if you want to transfer it to the string (representer by char*).

On the other hand, you need to have \0 at the end of the array, if you want to address it as char* and plan to use char* functions on it.



回答7:

'\0' in the array indicates the end of string, which means any character after this character is not considered part of the string.It doesn’t mean they are not part of the character array. i.e., we can still access these characters by indexing but they are just not part when we invoke string related things to this character array.

For a string to be in proper format and be able to work properly with the string functions, it must be a null-terminated character array. Without NULL, the programs show undefined behavior when we invoke string functions on the character array. Even though we might get lucky with the results most of the times, it still is an undefined behavior.



回答8:

It is string terminating symbol,When this is encountered ,compiler comes to know that your string is ended.