Donate

1 String - Array and String

Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0.
Because C has no built-in facilities for manipulating entire arrays (copying them, comparing them, etc.), it also has very few built-in facilities for manipulating strings.
In fact, C's only truly built-in string-handling is that it allows us to use string constants (also called string literals) in our code.
Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character.
For example, we can declare and define an array of characters, and initialize it with a string constant:
char string[] = "Hello, world!";
Two ways to initilize string >



In this case, we can leave out the dimension of the array, since the compiler can compute it for us based on the size of the initializer.
This is the only case where the compiler sizes a string array for us, however; in other cases, it will be necessary that we decide how big the arrays we use to hold strings.
An example program showing the character data type array:


Align CenterOut put of the program



In the above example, a character based array named word is declared, and each element of array is assigned a character.
The last element is filled with a zero value, to signify the end of the character string (in C, there is no string type, so character based arrays are used to hold strings).
A printf statement is then used to print out all elements of the array.

If you enjoyed this post, make sure you subscribe to my RSS feed!


1 comments to "String - Array and String"
Anonymous
on May 2, 2009 at 3:00 AM  

hello... hapi blogging... have a nice day! just visiting here....

Post a Comment