What You’ll Learn in This Hour:
What are strings?
Handling strings with C
The Arduino way to handle strings
Working with strings in your programs
One of the most confusing parts of the C programming language is working with text. Unfortunately, the C language uses a complex way of handling text, which can be somewhat confusing to beginner programmers. Fortunately, the Arduino developers realized that, and helped by creating some built-in string-handling features for the Arduino programming language. This hour first takes a look at how the C language handles strings, and then demonstrates how it’s much easier to work with strings in the Arduino environment.
Whereas computers only understand and work with numbers, humans need words to communicate ideas. The trick to interfacing computer programs with humans is to force the computer to produce some type of output that humans can understand.
Any time you need your program to interface with humans, you need to use some type of text output. To do that with a computer program requires the use of the character data type.
The character data type stores letters, numbers, and symbols as a numeric value. The trick comes in using a standard way of mapping numeric values to language characters. One of the more popular character mapping standards for the English-speaking world is the ASCII format.
With ASCII, each character in the English alphabet, each numeric digit, and a handful of special characters, are each assigned a numeric value. The computer can interpret the ASCII number representation of the character to display the proper text based on the value stored in memory.
To store a character in memory, you use the char
data type:
char letter = 'a';
When you use this statement, the Arduino doesn’t store the actual letter a in memory; it stores a numeric representation of the letter a. When you retrieve the letter
variable and display it using the Serial.print
function, the Arduino retrieves the numeric value, converts it to the letter a, and then displays it.
The next step in the process is storing words and sentences. A string value is a series of characters put together to create a word or sentence. The Arduino stores the multiple characters required to create the word or sentence consecutively in memory, then retrieves them in the same order they were stored.
The problem is that the Arduino needs to know just when a string of characters that make up the word or sentence stored in memory ends. Remember, the individual characters are stored as just numbers, so the Arduino processor has no idea of what number in memory represents a character and what doesn’t.
The solution is null-terminated strings. In the C programming language, when a string value is stored in memory, a 0
value is placed in the memory location at the end of the characters in the string value (this is called a null value). That way, the Arduino processor knows that when it gets to the null value, the string value is complete.
The next section shows just how to store and work with string values using the standard C programming language.
As you saw in the preceding section, the trick to creating strings in the C programming language is to store a series of characters in memory and then handle them as a single value. In Hour 7, “Programming Loops,” you were introduced to the idea of arrays. You can use character arrays to create and work with strings in the C programming language.
To refresh your memory, an array is a series of values of the same data type stored in memory, all associated with the same variable name. You can reference an individual value in the array by specifying its index value in square brackets in the variable name:
myarray[0] = 10;
The examples in Hour 7 only used numeric values in arrays, but you can use the same method with character values.
This section shows you how to use the C programming method to create and work with strings in your Arduino programs.
A string is nothing more than an array of character values, with the last array data value being a 0
to indicate the null terminator. You can create a string character array using several different formats. To define a simple string, you just create an array with the individual letters of the string, with a null character as the last array value:
char test[7] = {'A', ' ', 't', 'e', 's', 't', ' '),
Notice that each character (including the space) is defined as a separate data element in the array. Also, note that the array size that you define must be large enough to hold all the characters in the string plus the terminating null character (represented using the