Why adding a Null Character in a String array? [duplicate]
Asked Answered
I

2

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";
Irrupt answered 8/6, 2013 at 2:30 Comment(6)
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.Udella
now that is what i am looking for .Irrupt
Here's a Wikipedia article that gives you historical background: en.wikipedia.org/wiki/Null-terminated_stringMetallo
I think you meant char str[5] = {'A','N','S','\0'}; and char str1[5] = "ANS"; (if even the explicit lengths)Brasher
I edited your question because your code examples were wrong.Metallo
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.Stygian
A
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.

Audry answered 8/6, 2013 at 2:55 Comment(3)
NULL is a pointer value. NUL is an ASCII character. Char arrays are NUL terminated. Char pointers may have the value NULL.Rizika
C uses null character for '\0' and NULL for the null pointer constant. NUL is from ASCII to denote code 0. Recommend amending post.Fairish
Yes and no... NUL (ASCII 0d) is the abbreviation for Null, just like CR (ASCII 13d) is the abbreviation for Carriage Return.Audry
P
1

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

Parsimony answered 8/6, 2013 at 2:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.