As stated in the lesson about String (with capital S), you can use the String class, a special data type for Arduino ID, or make a string out of an array of characters. The characters in the array are formatted according to the ASCII characters. Another important element is that every string needs to have a final null character to tell other functions where the string ends. Every time we declare a string, we need to leave room for the terminating null character.
First, we dive into what a character is. Then we will discuss the array of strings (character string). In the end, we will finally our ready for some coding.
This lesson will focus on the string of characters. For more information about the String object, which is a bit easier (at the cost of more memory usage), see the String object tutorial.
What is a character?
The first element is of the character string that we need to learn is the character data type. Characters have a capacity of one-byte memory allocation. It can store values between 0 and 255. Characters are stored as numbers but are encoded as ASCII text. The ASCCI chart allows you to present text, symbols, or characters as numbers.
Each letter has a number associated with it. Each numeric value also has a number associated with it. With ASCII encoding, you can write down some numbers and pass it to a Serial Monitor or another Arduino board. With the ASCII chart, you can encode these numbers.
To declare a character, you just have to use the word char to declare the character and attach a variable name that holds the character. Next, you initialize the character by using an equal sign and the character that you would like.
char Letter = 'A';
Another way of representing the letter A is to use the number associated with it in the ASCII chart. So we could rewrite char Letter with the same end result. The variable name Letter is now holding the number 65, which is encoded into ASCII.
char Letter = 65;
So, there are two possibilities when using a single character. You could write initialize with the number from the ASCII chart or use the letter or number you want, with single quotes, and the encoding is done for you by the compiler.
It seems a bit awkward to use a number to encode a number. But in fact, what you do is using a symbol that represents a number. Below is the table of the ASCII characters. This is not the full ASCII table.
Table 1, ASCII characters
what is a character string
Followed from characters comes an array of characters called string or c-string. A string, with lowercase s, is a couple of characters in a row. The characters in the row are enclosed with double quotes “ “. The string looks a bit like declaring and initializing an array. A simple character array might look as the following:
char Greeting = "Hello everyone";
The example above is a c-style text string with a char data type that contains the text “Hello everyone.” The length of the array is equal to all the different characters in the array.
If you count the letters and spacing, you will see that there are only 14 characters in the string. Because strings need a null terminating character or 0 byte that tells other functions that the string ends. Without the null character functions like Serial.println() would continue to access parts of the memory used for other things. When you declare a string without a number of elements, the compiler will determine the array’s proper dimension.
So, It seems that the string has a collection of 14 elements, each stored as a separate element in the array. Our string named Greeting will be sized with 15 elements and, therefore, will contain 20 bytes.
You could also rewrite the string above to determine the dimension size when you declared the string.
char Greeting = "Hello everyone";
Ensure that your string’s size (when you declare it yourself), dimension, is one larger than the number of elements in the string. There are many more ways to declare strings, and we will look at them below.
Terminating null character: This allows functions like Serial.print() to tell where the end of the string is. You can set the dimension of the string yourself or let the compiler do it for you.
Creating a Character string
Creating a Character string: is very easy to do. Below are some ways you could create a c-string. The code below shows some examples of how you can declare strings.