Declaration and Initialization of Strings in C Language with Free Notes

The Strings are an Array of characters. SO they are generally declared like arrays but they have one property that is different from normal arrays.

The Last character in a Strings is end of String (EOS) marker, the ‘\0’ character.

So when you declare Strings, You must allocate an extra character for this marker.

Free E-Books

An example of array declaration is: –

char item [9];

where item is the name of the one dimensional character array having maximum 9 characters, 8 for the alphabets and one for the null character.

NOTE: – Since most strings vary in length, the end of string marker i.e. ‘\0’ is used to determine the length of a string at any point in a program.

As other data types, strings can also be initialized at the same place where they are declared e.g.

char item[9] = "COMPUTER";
       OR
char item[9] = {'C','O','M','P','U','T','E','R', '\0'};

If we initialize char item[8] =”COMPUTER”;

then the result may be unpredictable because the array size id less than subscript. And character may be placed on some of the important data or program and will disturb the other data or program. There will be no error by the compiler for this but there will be a warning that you are going beyond the array size.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top