Variables of type char can hold only a single character, so they have limited usefulness. You also need a way to store strings, which are sequences of characters. A person’s name and address are examples of strings.
- Although there is no special data type for strings, C handles this type of information with arrays of characters terminated by ‘\0’ character.
Declaring string variable:
- The general form of declaration of string variable is:
char city[size]; //Static Memory Allocation
char *city = (char*) malloc (sizeof(char) * size); //Dynamic Memory Allocation
- The size determines number of characters in string_name.
char city[10];
char *city = (char*) malloc (sizeof(char) * 10);
Initialization of Character Arrays:
- Every string must be terminated by a null hence Array must be defined in such a way that it can hold one byte larger than the largest string, to make a room for the null.
- A string can be initialized at the time of array declaration or using a loop in the program.
- Like other C data types, character arrays can be initialized when they are declared.
char name[10] = { ‘A’, ‘l’, ‘a’, ‘b’, ‘a’, ‘m’, ‘a’, ‘\0’ };
char *name = { ‘A’, ‘l’, ‘a’, ‘b’, ‘a’, ‘m’, ‘a’, ‘\0’ };
- It’s more convenient, however, to use a literal string, which is a sequence of characters enclosed in double quotes:
char string[10] = “Alabama”;
char string[] = “Alabama”;
char *name = “Alabama”;
Note: When you use a literal string in your program, the compiler automatically adds the terminating null character at the end of the string.
- If you don’t specify the number of subscripts when you declare an array, the compiler calculates the size of the array for you. Thus, the following line creates and initializes an eight-element array:
- If the number of characters in the string exceed the maximum size, the string is truncated.
- If the number of characters in the string are less than the maximum size, the compiler allocates the null character at the first empty element of the string.
Comparing char[] with char*
Using character array | Using character pointer |
1: We cannot assign a string to another char str1[]=”Hello”; char str2[10]; str2 = str1 //This statement will give error incompatible types in assignment |
1:We can assign a char pointer to another char pointer char *s = “Good morning” char *p; p = s; //works fine |
2: Once string is initialized it cannot be assigned to another set of character. char str1[]=”Hello”; str1=”bye”; //Give error |
2: We can assign another set of character in case of pointers. char *s=”Hello”; s = “bye”; //works fine |
Reading and Writing Strings:
- You can read strings using scanf function.
Ex: scanf(“%s”,name); where name is a character array or character pointer.
- scanf is not capable of receiving multiword strings. Therefore strings such as “Hello John” is unacceptable. To overcome this limitation gets() function can be used.
Ex. gets(name);
- You can write strings using puts() function or printf() function.
Ex.puts(name) or printf(“%s”,name). Here ‘%s’ is conversion specifier for string.