Character Strings in C

Danny Padron
4 min readMar 27, 2021

In the C programming language when we talk about a string constant or a string literal what that means is that it is a sequence of characters or symbols between a pair of double quotes " " . Anything between the quotes is interpreted by the compiler as a string. C doesn’t have any special operators in the language for processing strings but the standard library does provide an extensive number of different functions that can be performed on strings. Before we look at those functions we first need to define a string within C, to declare a string in C you have to simply use the char type followed by the name you want to give your string and then brackets after the name to indicate the size of the string. For example the syntax to simply declare the string would look like this :

char myString[20];

What this means is that the string myString will have a total of 20 memory cells. Each string in C ends with what is called a null character, not to be confused with NULL. A null character takes up one element of the 20 cells that were allocated for myString , so really myString can be a string that has up to 19 characters. The null character looks like \0 , this indicates to the compiler that the string has been terminated at this character. The null character doesn’t have to be added into every string, the C compiler will automatically add \0 to the end of every string constant. So when you specify the dimension of an array that you intend to use to store a string, it must be at least one greater than the number of characters in the actual string that needs to be stored.

After defining our string then we need to initialize it, the same as any other array initialization, because a string is essentially an array of characters. String variables can be initialized when they are declared like so :

char word[] = {'H', 'e', 'l', 'l', 'o', '!'};

This is one way to initialize the string variable, in the absence of a particular array size, the C compiler will automatically count the number of elements in the array and it would reserve space for 7 characters, because it will automatically add an extra space for the null character. You could also explicitly define the size of the string but you always have to remember to leave enough space for the null character.

char word[6] = {"Hello!"};

If you specify the size of an array and is it too small and the compiler can’t add the null character to the end of the string it won’t put one there. The best thing to do is to not specify the size of the string variable and let the compiler itself calculate the size and then you can be sure that it will be correct. Once the string variables are declared and initialized they can be used if you wanted to display your string it would look like this :

printf("The word is: %s", word);

To display the string we use the %s and this lets the compiler know that it is a null-terminated string and will display it. The same %s can be used if you wanted to have a string inputted by the keyboard using the scanf function.

char input[10];
printf("Enter your name: ");
scanf("%s", input);

Lets take a look at some of the common functions that can be used on strings. The first common function we will look at is strlen() this function will give us the length of a given string the syntax for it would look like this:

#include <stdio.h>
#include <string.h>
int main() {
char myString[] = "my string";
printf("The length of this string is: %d", strlen(myString)); return 0;
}

When using string functions we need to put #include <string.h> so that we can use the functions from the C library for strings. The result that we would get from printf is 9 it will count the space as a character and will not add the null character to this count. Another common function that can be used on strings in C is strcmp() this is used to compare two strings. This function does for strings what relational operators do for numbers, it will return 0 if the two strings being compared are the same and will return nonzero otherwise. The last common function we will look at is the strchr() which searches a given string for a specified character. This function takes in two arguments the first being the string that we want to search and the second argument is what character we are looking for. Here is an example of how to use this function :

char myString[] = "The puppy ate it's food"  // The string to search
char lookFor = 'a'; // The character we are searching for
char *pLookFor = NULL; // Pointer initialized
pLookFor = strchr(myString, lookFor); // Stores address of 'lookFor' in the 'pLookFor' pointer

Using strchr(myString, lookFor) will locate the address location of the first character that the compiler will find that matches the lookFor character. We then created and initialized a pointer so that we can have a location to store the address location for the character that we searched for within the string. The amount of functions that the C library has for strings is extensive and we haven’t even scratched the surface on these functions. These are just some of the more common ones. A few reminders about strings in C is that there is a difference between "a" and 'a' . "a" is a string constant or a string literal while 'a' is a character constant, also "a" is really two characters ( ‘x’ and ‘\0’) . When using these string functions in your C program always remember to include the C library for the string functions ( #include <string.h> ). If there is something that you want to do to a string most likely this library has a function that will work for you.

--

--

Danny Padron

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!