Strings in C can be declared as character arrays that are terminated with a null character '\0'. Strings can be initialized by assigning character constants in quotes or curly braces. Common string library functions allow input of strings with scanf and gets, getting the length with strlen, concatenating with strcat, comparing with strcmp, and copying with strcpy.
Introduction
1. An arrayis a collection of data items, all of the
same type, accessed using a common name.
2. Strings are defined as an array of characters.
3. The difference between a character array and a
string is the string is terminated with a special
character '0'. Declaration of strings: Declaring a
string is as simple as declaring a one
dimensional array.
4. C language does not directly support string as a
data type. Hence, to display a string in 'C', you
need to make use of a character array.
4.
Declaration/Initialization of anArray
Syntax for declaring a variable as a string is as
follows:
char string_variable_name [array_size];
Example:
char first_name[15]; //declaration of a string variable
char last_name[15];
The C compiler automatically adds a NULL character '0' to
the character array created.
5.
Declaration/Initialization of anArray
Following example demonstrates the initialization of a
string variable:
• char first_name[15] = "ANTHONY";
• char first_name[15] = {'A','N','T','H','O','N','Y','0'};
// NULL character '0' is required at end in this declaration
• char string1 [6] = "hello";
/* string size = 'h'+'e'+'l'+'l'+'o'+"NULL" = 6 */
• char string2 [ ] = "world";
/* string size = 'w'+'o'+'r'+'l'+'d'+"NULL" = 6 */
• char string3[6] = {'h', 'e', 'l', 'l', 'o', '0'} ;
/*Declaration as set of characters ,Size 6*/