Published by: Nuru
Published date: 21 Jun 2021
Character Array and Strings are the characters arranged one after the other inside the computer memory. The character array is a string. The string is a sequence of characters that are treated as a single data item. There is the termination of strings always by using a null character '\0'. We should remember that the C language does not support strings as a data type. It is actually a one-dimensional array of characters in C language. These are often used to create meaningful and readable programs.
There are different operations on character strings and some of them include:
Syntax:
char string_name[size];
Here, the size determines the number of characters of strings in the string_name.
For Example:
char marks [12];
When the compiler assigns a character string to the character array, it automatically gives it a null character i.e. “\0” at the end of it. The size is equal to the number of characters of the strings. In addition to this, we give one more size extra because of the null character.
We can also initialize the strings in the following two forms like below:
char name [6] = { ‘N’, ‘O’, ‘T’, ‘E’, ‘S’, ‘\0’ };
char name [ ] = { ‘N’, ‘O’, ‘T’, ‘E’, ‘S’, ‘\0’ }; or char name [ 13 ] = {‘O’, ‘N’, ‘L’, ‘I’, ‘N’, ‘E’, ‘ ‘, ‘N’, ‘O’, ‘T’, ‘E’, ‘S’, ‘\0’ };
Now, these will become,
char name [ ] = “NOTES”;
char name [13 ] = “ONLINE NOTES”;
When we initialize the character array by listing the elements, the null element should be provided correctly. Or, the size of the array should be accurate.
For Instance, Let us write a program for initializing the string variables.
And. the output comes like below:
O N L I N E N O T E S