String is a collection of character or group of character, it is achieve in C language by using array character. The string in C language is one-dimensional array of character which is terminated by a null character '\0'. In other words string is a collection of character which is enclose between double cotes ( " " ).
Note: Strings are always enclosed within double quotes. Whereas, character is enclosed within single quotes in C.

Declaration of string

Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.

Important points for Declaration of string

  • In declaration of string size must be required to mention otherwise it gives an error.


char str[];   // Invalid
char str[10]; // Valid
  • In declaration of the string size must be unsigned integer value (not -ve or zero value) which is greater than zero only.


char str[];   // Invalid
char str[0];  // Invalid
char str[-1]; // Invalid
char str[10]; // Valid


char variable_name[SIZE];
char str[5];

Initializing Array string

According to array initialization concept


char str[]="abcd";
char str[5]="abcd";
char str[5]={'a','b','c','d','\0'};
char str[]={'a','b','c','d','\0'};
char str[5]={'a','b','c','d','\0'};

Important points for Initialization of the string

  • In Initialization of the string if the specific number of character are not initialized it then rest of all character will be initialized with NULL.


char str[5]={'5','+','A'};
    str[0];  ---> 5
    str[1];  ---> +
    str[2];  ---> A
    str[3];  ---> NULL
    str[4];  ---> NULL
  • In initialization of the string we can not initialized more than size of string elements.


char str[2]={'5','+','A','B'};  // Invalid
  • In initialization of the string the size is optional in this case how many variable elements are initialized it, that array element will created.


char str[]={'5','+','A','B'};  // Valid
sizeof(str)  --> 4byte
When we are working with character array explicitly NULL character does not occupies any physical memory at the end of the character array.


char str[]={'h','e','l','l','o'};
sizeof(str)  --> 5byte
String data at the end of the string NULL character occupies physical memory.


char str[]="hello";
sizeof(str)  --> 6byte

Reading String from user


char str[5];
Note: String variable str can only take only one word. It is because when white space is encountered, the scanf() function terminates.


char str[5];


gets() are used to get input as a string from keyword, using gets() we can input more than one word at a time.

Example of String program in c language



void main()
char str[10];
cout<<"Enter any string: ";
cout<<"String are: ";
Explanation: Here gets() function are used for input string and puts() function are used to show string on console or monitor.


Enter String: hello word
String are: hello word

C Library String functions

All the library function of String is available in String.h header file.
1strcpy(s1, s2)Copies string s2 into string s1.
2strcat(s1, s2)Concatenates string s2 onto the end of string s1.
3strlen(s1)Returns the length of string s1.
4strcmp(s1, s2)Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5strchr(s1, ch)Returns a pointer to the first occurrence of character ch in string s1.
6strstr(s1, s2)Returns a pointer to the first occurrence of string s2 in string s1.