Hide minor edits - Show changes to markup
Strings are represented as arrays of type char and are null-terminated.
Text strings can be represented in two ways. you can use the String data type, which is part of the core as of version 0019, or you can make a string out of an array of type char and null-terminate it. This page described the latter method. For more details on the String object, which gives you more functionality at the cost of more memory, see the String object page.
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.
It is often convenient, when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.
Generally, strings are terminated with a null character (ASCII code 0). This allows function (like Serial.print()) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.
This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like Serial.print()) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.
This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled with a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an expample of a two-dimensional array.
It isn't necessary to understand this construction in detail to use it effectively. The code fragments below illustrate the idea.
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.
In the code below, the asterisk after the datatype char "char*" indicates that this is an array of "pointers". All array names are actually pointers, so this is required to make an array of arrays. Pointers are one of the more esoteric parts of C for beginners to understand, but it isn't necessary to understand pointers in detail to use them effectively here.
Serial.print(myStrings[i]);
Serial.println(myStrings[i]);
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array.
It isn't necessary to understand this contruction in detail to use it effectively. The code fragments below illustrate the idea.
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an expample of a two-dimensional array.
It isn't necessary to understand this construction in detail to use it effectively. The code fragments below illustrate the idea.
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",//
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",
"This is string 4", "This is string 5","This is string 6",
};
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",// "This is string 4", "This is string 5","This is string 6"};
It is often useful when working with several strings, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array. The code fragments below illustrate the idea.
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array.
It isn't necessary to understand this contruction in detail to use it effectively. The code fragments below illustrate the idea.
char* myStrings[]={"This is string 1", "This is string 2"};
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",
"This is string 4", "This is string 5","This is string 6",
};
Serial.print(myStrings[0]); delay(100); Serial.print(myStrings[1]); delay(100);
for (int i = 0; i < 6; i++){
Serial.print(myStrings[i]); delay(500); }
Possibilities for declaring strings
Possibilities for declaring strings
Single quotes or double quotes?
Single quotes or double quotes?
Wrapping long strings
Wrapping long strings
Arrays of strings
It is often useful when working with several strings, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually a two-dimensional array. The code fragments below illustrate the idea.
char* myStrings[]={"This is string 1", "This is string 2"};
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print(myStrings[0]);
delay(100);
Serial.print(myStrings[1]);
delay(100);
}
Null termination
Null termination
Strings in the C programming language are represented as arrays of type char.
Strings are represented as arrays of type char and are null-terminated.
Strings in the C programming language are represented as arrays of chars.
Strings in the C programming language are represented as arrays of type char.
This means that your string needs to have space for more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
Strings in the C programming language are defined as arrays of type char.
Strings in the C programming language are represented as arrays of chars.
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
Note the differences between Str2 & Str3, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2.
When declaring a string, you must declare an extra character for this null or the compiler will complain with an error about the initialization string being too long. For the same reasons, Str3 can hold only 14 characters, not 15, as one might assume.
Generally, strings are terminated with a null character (ASCII code 0). This allows function (like Serial.print()) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.
This means that your string needs to have space for more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled by a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
Note that it's possible to have a string without a final null character (e.g. if you had specified the length of Str2 as seven instead of eight). This will break most functions that use strings, so you shouldn't do it intentionally. If you notice something behaving strangely (operating on characters not in the string), however, this could be the problem.
char myString[] = {"This is the first line"
char myString[] = "This is the first line"
" etcetera"};
" etcetera";
Wrapping long strings
You can wrap long strings like this:
char myString[] = {"This is the first line"
" this is the second line"
" etcetera"};
All of the following are valid declarations of valid strings.
All of the following are valid declarations for strings.
Facts about strings
Strings can be declared in any of the following manners
All of the following are valid declarations of valid strings.
char Str1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char Str3[4];
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
Note the differences between Str1 & Str2, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2. If you haven't declared an extra character for this null, the compiler will complain with an error about the initialization string being too long.
In the first example the array is initialized with constants of type char, 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing them in single quotes. In example four, the initialization of the string is shown with the whole word in quotation marks as in "arduino"
Note that in example 4 above, the
long time;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
}
long var = val;
Facts about strings
Possibilities for declaring strings
Null termination
Note the differences between Str2 & Str3, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2.
When declaring a string, you must declare an extra character for this null or the compiler will complain with an error about the initialization string being too long. For the same reasons, Str3 can hold only 14 characters, not 15, as one might assume.
Single quotes or double quotes?
Strings are always defined inside double quotes ("Abc") and characters are always defined inside single quotes('A').
char myStr1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char myStr2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr3[4];
char myStr4[ ] = "arduino";
char myStr5[8] = "arduino";
char myStr6[15] = "arduino";
char Str1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char Str3[4];
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
A couple of things to note.
Note the differences between Str1 & Str2, in theory it seems the array of Str1 should be able to be contained with a declaration of 7 elements in the array, since there are only 7 letters in "Arduino". However the Arduino language enforces "null termination" meaning that the last character of an array must be a null (denoted by \0), as in Str2. If you haven't declared an extra character for this null, the compiler will complain with an error about the initialization string being too long.
char myStr[7] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char myStr[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr[4];
char myStr[ ] = "arduino";
char myStr[7] = "arduino";
char myStr[15] = "arduino";
char myStr1[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char myStr2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr3[4];
char myStr4[ ] = "arduino";
char myStr5[8] = "arduino";
char myStr6[15] = "arduino";
In the first example the array is initialized with constants of type char 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing in single quotes. In example four, the initialization of the string is shown with the whole word in quotation marks
In the first example the array is initialized with constants of type char, 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing them in single quotes. In example four, the initialization of the string is shown with the whole word in quotation marks as in "arduino"
char str[5] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char str[6] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char str[3];
char str[ ] = "arduino";
char str[7] = "arduino";
char str[15] = "arduino";
char myStr[7] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char myStr[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char myStr[4];
char myStr[ ] = "arduino";
char myStr[7] = "arduino";
char myStr[15] = "arduino";
In the first example the array is initialized with constants of type char.
In the first example the array is initialized with constants of type char 'a', 'r'. 'd', etc. These constants (letters in this case, but any ASCII symbols really) are declared by enclosing in single quotes. In example four, the initialization of the string is shown with the whole word in quotation marks
Strings in the C programming language are defined as arrays of type char.
Strings can be declared in any of the following manners
char str[5] = {'a', 'r', 'd', 'u', 'i' 'n' 'o'};
char str[6] = {'a', 'r', 'd', 'u', 'i', 'n', 'o','\0'};
char str[3];
char str[ ] = "arduino";
char str[7] = "arduino";
char str[15] = "arduino";
A couple of things to note. In the first example the array is initialized with constants of type char.
Note that in example 4 above, the
long time;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
}
long var = val;