12

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...

ShuklaSannidhya
  • 8,572
  • 9
  • 32
  • 45
  • 5
    `\0` indicates the end of a string – jbowes Dec 16 '12 at 18:16
  • 1
    Local variables are not initialized in C. Thus, the local variable `line` has garbage where ever you didn't write to it. If the garbage happens to be `0` then your program will work without explicitly writing the null. However, if you do another `readline` into the `line` variable, and make this one a shorter line than the first, you'll see the remnants of the first line at the end of the second in `line`. Writing the null character at the end will prevent that. – Erik Eidt Dec 16 '12 at 20:18

9 Answers9

16

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).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    @SandyLee_user53167 you were lucky this time :) – Maroun Dec 16 '12 at 18:20
  • 1
    @SandyLee_user53167 the `copy` function in your code runs until it sees the `\0` character. – rohit89 Dec 16 '12 at 18:26
  • no, absolutely not. You will cause indeterminate behaviour. It might appear to work in this simple example but that's pure luck. On a different OS or compiler, or if you were doing something more complex like copying the string, you would cause a memory scribble - writing over parts of memory it's not supposed to. Try adding something like defining a character variable before and after you define `longest` (eg `char x='Z';`)and see if your program still does what you expect.. – Steve Atkinson Dec 16 '12 at 18:28
1

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

Community
  • 1
  • 1
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
1

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.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • Nota bene: Meanwhile, the popular (well, more or less) language Delphi introduces various types of strings of which only one has the 255 char limitation. All the others (ansi, unicode, wide etc.) have a limitation of (I think) 4 GB. – alzaimar Dec 16 '12 at 18:53
  • @alzaimar Well, certainly more popular than *per se* pascal these days. Don't know it myself. Either way, the programmer can choose his or her poison, which is always nice. – dmckee --- ex-moderator kitten Dec 16 '12 at 19:03
0

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

ouah
  • 142,963
  • 15
  • 272
  • 331
0

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.

Christoffer
  • 12,712
  • 7
  • 37
  • 53
0

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.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
0

'\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.

0

I've just looked it up If your array is considered as string Which means like this char array[MAX]="string"; Or like this scanf("%s",array); Or char* table; Then the NULL character '\0' will append automatically as the end of the characters on that table But if you initialized it like this char array[MAX]={'n','o','t','s','t,'r'}; Or you fill it using character by character with %c format

for(int i=0;i<MAX;i++)
   scanf("%c",&array[i]);

Or getchar() instead of scanf("%c",...) Then you have to add '\0' by yourself Because now it considered as any other array's type (int,float...) So the cases that we consider as empty are actually filled by random numbers or characters depends on the type Meanwhile in the case of a string type the next character after the last considered character is by default '\0' for more explanation the length of this char array[]="12345" is 6 The array[5]=='\0' will return 1 by other words you can't define a string array like this char array[3]="123" because we left no room for the '\0' that has to append automatically last example char array[7]={'t','e','s','t','\0'}; Here array[4] is the NULL character array[5] and array[6] are random values But if it was string then "test" array[4] and 5 and 6 are all filled by the NULL character (NULL character can refers to any white_space as I think so tab '\t' and enter '\n' are also NULL characters just like '\0' which may refer to spacebar) nota ben: we can't assign array[7] or more as we all know but if you try to output it, it'll show a random value as any empty case

Frogsam
  • 1
  • 1
-1

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

Ehsan
  • 9
  • 1
  • 6