What is C-string? Explain with an example.
- C-string is null terminated string which is simply a null terminated character array. C-string is essentially C style string. It is different than the way C++ handles strings-by providing string class.
- Its general form of declaration is :
char var_name[size];
var_name is name given to the array variable.
- Please note while declaring a C-string, we need to declare it to be one character longer that the largest string that it is to hold. For example, to declare a C-string str that can hold 10 characters, we need to write :
char str[11];
- One extra location is reserved to store the null char at the end of the string.
- Some of the most commonly used C-string functions are :
strlen(s1) returns length of s1
strcpy(s1, s2) copies s2 into s1
strcmp(s1,s2) returns 0 if s1 & s2 are same, less than 0 if
s1 < s2, greater than 0 if s1 > s2.