6

I know that we have to use a null character to terminate a string array like this:

char str[5] = { 'A','N','S','\0' };

But I just wanted to know why is it essential to use a null character to terminate an array like this?

Also why don't we add a null charater to terminate these :-

char str1[5]="ANS";
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
Anurag-Sharma
  • 4,278
  • 5
  • 27
  • 42
  • 4
    Because a null takes one byte, whereas storing the length of the string with the string itself could take multiple bytes. Memory was scarce back in the day, so the smaller solution won out. – dlev Jun 08 '13 at 02:34
  • now that is what i am looking for . – Anurag-Sharma Jun 08 '13 at 02:34
  • Here's a Wikipedia article that gives you historical background: http://en.wikipedia.org/wiki/Null-terminated_string – xxbbcc Jun 08 '13 at 02:47
  • 1
    I think you meant `char str[5] = {'A','N','S','\0'};` and `char str1[5] = "ANS";` (if even the explicit lengths) – chris Jun 08 '13 at 02:48
  • I edited your question because your code examples were wrong. – xxbbcc Jun 08 '13 at 02:50
  • Also, you don't need it in `str1[5]="ANS"` because the compiler puts it there for you. The string constant "ANS" allocates 4 bytes, and puts a zero terminator in the last one. – Lee Daniel Crocker Jun 08 '13 at 09:12

2 Answers2

9

The NULL-termination is what differentiates a char array from a string (a NULL-terminated char-array) in C. Most string-manipulating functions relies on NULL to know when the string is finished (and its job is done), and won't work with simple char-array (eg. they'll keep on working past the boundaries of the array, and continue until it finds a NULL somewhere in memory - often corrupting memory as it goes).

In C, 0 (the integer value) is considered boolean FALSE - all other values are considered TRUE. if, for and while uses 0 (FALSE) or non-zero (TRUE) to determent how to branch or if to loop. char is an integer type, an the NULL-character (\0) is actually and simply a char with the decimal integer value 0 - ie. FALSE. This make it very simple to make functions for things like manipulating or copying strings, as they can safely loop as long as the character it's working on is non-zero (ie. TRUE) and stop when it encounters the NULL-character (ie. FALSE) - as this signifies the end of the string. It makes very simple loops, since we don't have to compare, we just need to know if it's 0 (FALSE) or not (TRUE).

Example:


    char source[]="Test";  // Actually: T e s t \0 ('\0' is the NULL-character)
    char dest[8];

    int i=0;
    char curr;

    do {
       curr = source[i];
       dest[i] = curr;
       i++;
       } while(curr); //Will loop as long as condition is TRUE, ie. non-zero, all chars but NULL.

Baard Kopperud
  • 577
  • 1
  • 5
  • 12
1

It isnt essential but if you are using any of the standard libraries, they all expect it.

Tom Studee
  • 10,316
  • 4
  • 38
  • 42