Summary: in this tutorial, you will learn about C string and how to manipulate strings effectively in your program.
String definition
In C, a string is a sequence of characters. C does not provide any special type for storing strings like other programming languages such as Perl, Python, and PHP. C only provides character type, therefore, a C string can be defined as an array of characters.
Null-terminated string
In C, a string is terminated by a special character called null character ( \0
). When you define a string, you must reserve a space for the ( \0
) null character. For example, to declare a string that holds 5 characters, the size of the array must be at least 6.
1 | char str[6]; |
Notice that in the ASCII table, the ( \0
) null character has a value of 0
.
C String declaration
You can declare a string as an array of characters or a pointer that points to the first character of a memory block that holds the string.
C string as an array of characters
The first way to declare a string is to declare it as an array of characters as follows:
1 | char str[6]; |
You can also initialize a string when you declare it:
1 | char s[6] = "Hello"; |
When you use a string literal, the compiler will add the \0
null character at the end of the string automatically so you don’t have to specify it explicitly.
You can omit the size of the array as follows:
1 | char str[] = "Hello"; |
The compiler creates an array of 6
characters that is sufficient enough to hold the Hello
text and the \0
null character.
C string as a pointer
The second way to declare a string is using a pointer as follows:
1 | char* s; |
The statement declared a pointer that points to a variable with char
type. It does not point to anything . However you can initialize it by pointing it to a literal string:
1 | char* s = "This is a string in C"; |
When this statement executes, the literal string This is a string in C
is created somewhere in the memory and the s
pointer points to the first character of this string.
Declaring a string in this way, the size of the string is fixed. If you want the size of the string to be dynamic and determined at run-time, you need to use the malloc()
function that allocates memory for the string at run time. Check it out the dynamic memory allocation for more information on how to use the malloc()
function.
C string operations
Copying string
To copy a string into another string, you use the strcpy()
function. The following example shows you how to copy a string into another.
1 2 3 4 5 | char lastname[25]; char firstname[25]; strcpy(lastname,"John"); strcpy(firstname,"Doe"); |
C string concatenation
To concatenate two strings in C, you use the strcat()
function. The following example copies the lastname
string to the fullname
string, and concatenates the fullname
string with “, ” literal string and the firstname
string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> #include <stdlib.h> int main() { char lastname[25]; char firstname[25]; char fullname[51]; strcpy(lastname,"John"); strcpy(firstname,"Doe"); /* copy lastname string to fullname */ strcpy(fullname,lastname); /* concatenate fullname with ", " and firstname */ strcat(fullname,", "); strcat(fullname,firstname); printf("%s",fullname); /* John, Doe */ } |
C string compare
To compare two strings in C, you use the strcmp()
function. See the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> #include <string.h> #define MAX 100 int main() { char s1[MAX]; char s2[MAX]; printf("Enter the first string:\n"); fgets(s1,sizeof(s1),stdin); printf("Enter the second string:\n"); fgets(s2,sizeof(s2),stdin); int result = strcmp(s1,s2); if(result > 0) { printf("s1 > s2"); } else if(result < 0) { printf("s1 < s2"); } else { printf("s1 = s2"); } return 0; } |
Looping over a string
You can loop over a string using subscript. The following example shows you how to loop over a string using subscript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <stdio.h> #include <stdlib.h> int main() { /* loop over a string using subscript*/ char *s = "C string"; int i; for(i = 0; i < strlen(s); i++) { printf("%c",s[i]); } return 0; } |
Notice that we used the strlen()
function to get the length of the string.
You can also use a pointer to loop over a string. You use a char pointer that points to the first character of the string, and iterates it until the \0
null character is reached.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <stdio.h> #include <stdlib.h> int main() { /* loop over a string using subscript*/ char *s = "String in C"; char* ps = s; while(*ps != '\0') { printf("%c",*ps); ps++; } } |
In this tutorial, you have learned about the C string, and how to manipulate strings using C built-in functions.