Character arrays and Strings in C


It is a general misconception that both character arrays and strings are one and the same. But, THEY ARE NOT THE SAME. If you already know the difference between them, then I hope this post will add a little more to what you already know.


What are character arrays and strings?

let's first see what they have in common. A character array and string are both sequence of characters i.e, they store the base address of multiple characters  in sequence and these characters can be accessed using index value.

Here is what happens when you initialize a character array:
//character array:

char ch_arr[3] = "abc";

/* DESCRIPTION:
*here,
*ch_arr[0] stores 'a'
*ch_arr[1] stores 'b'
*ch_arr[2] stores 'c'
*/

Here's what happens when you initialise a string:
//string:

char str[] = "abc";
/* DESCRIPTION: *here, *str[0] stores 'a' *str[1] stores 'b' *str[2] stores 'c' * additionally str[3] stores '\0'(nul character) */

From the above two block, you can easily find the difference between a character array and string. 
A string is a sequence of characters terminated by a null character i.e, '\0'.
To put it more simply A string is a character array with its last character as '\0'.   


What's the use of null terminating character?

The null terminating character('\0') in string acts a natural full-stop and this helps in printing the sequence of characters stored in a string at one go which cannot be done with character arrays.

for example, see this:
char char_arr[3] = {'a','b','c'}; //character array
char str[] = "abc";               //string

printf("output : %s\n", char_arr);  //printing character array
printf("output : %s\n", str);       //printing string

/*
OUTPUT:

output : abcÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ(ŽK
output : abc

*/

Here you could see that when we print a character array at one go, undesired characters are printed after the first three elements.WHY? This is because there is no null terminating character to mark the end and the sequence of characters. So the compiler goes on printing till a null terminating character is encountered which is undefined behaviour. But on the other hand if you look at the output of the string `str`, the output is just as desired and this is due to the null terminating character present at the end of the sequence of characters.

If you want to print all the characters of a character array then a loop must be used and only index values less than the size of the array must be accessed to avoid undefined behaviour and here characters are printed one-by-one:
char char_arr[3] = {'a','b','c'}; //character array

printf("output : ");

for(int index = 0; index < 3; index++) //printing characters of array one-by-one
{
    printf("%c", char_arr[index]);
}

printf("\n");

/*
OUTPUT:

output : abc

*/

If you try to access the `index_values >= size_of_character_array`, the behaviour is undefined and thus it must be avoided.

Finally, a character array can be made into a string by providing a null character at the end this way:
char char_arr[4] = {'a','b','c','\0'}; //character array as string!

//NOTE: EXTRA BYTE FOR `nul character`, so size must be `no_of_characters + 1`

printf("output : %s", char_arr);

/*
OUTPUT:

output : abc

*/


Strings have a myriad of uses in programming. For example, the command line arguments that you send are stored as strings and strings are very helpful in text processing. There's a special header file called string.h to manipulate the strings.

Hope this post has been educational to you :)

Comments

Popular posts from this blog

Beginner's guide to Solving the N-Queens problem using backtracking method

PvP Chain reaction game using Python

Guide to Solving Maximal Subarray Problem using Kadane's Algorithm