C String

Summary: in this tutorial, you will learn about C string and how to manipulate strings effectively in your program.

C StringString 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.

Notice that in the ASCII table, the ( \0) null character has 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:

You can also initialize a string when you declare it:

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:

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:

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:

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.

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.

C string compare

To compare two strings in C, you use the strcmp() function. See the following example:

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:

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 iterate it until the \0 null character is reached.

In this tutorial, you have learned about the C string, and how to manipulate strings using C built-in functions.