Hide minor edits - Show changes to markup
[@
(:source lang=arduino tabwidth=4:)
@]
(:sourceend:)
Serial.println(myStr[i], BYTE);
Serial.write(myStr[i]);
Serial.println();
delay(5000); // slow down the program
Note also that a properly formatted string ends with the NULL symbol, which has ASCII value 0.
sizeof (variable)
sizeof variable
Both forms are accepted by the compiler
sizeof(variable)
[@ for (i = 0; i < (sizeof(myStr)/sizeof(int)) - 1; i++) @)
for (i = 0; i < (sizeof(myInts)/sizeof(int)) - 1; i++) {
// do something with myInts[i]
}
This program prints out the text string a character at a time. Try changing the text phrase.
The sizeof operator is useful for dealing with arrays (such as strings) where it is convenient to be able to change the size of the array without breaking other parts of the program.
This program prints out a text string one character at a time. Try changing the text phrase.
The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array, to find the
The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.
(type)(variable)
type: any variable type (e.g. int, float, byte)
variable: any variable or constant
sizeof (variable)
sizeof variable
Both forms are accepted by the compiler
variable: any variable type or array (e.g. int, float, byte)
This program prints out the text string a character at a time. Try changing the text phrase.
char myStr[] = "this is a test";
float f;
f = 3.6; i = (int) f; // now i is 3
void setup(){
Serial.begin(9600);
}
void loop() {
for (i = 0; i < sizeof(myStr) - 1; i++){
Serial.print(i, DEC);
Serial.print(" = ");
Serial.println(myStr[i], BYTE);
}
}
When casting from a float to an int, the value is truncated not rounded. So both (int) 3.2 and (int) 3.7 are 3.
Note that sizeof returns the total number of bytes. So for larger variable types such as ints, the for loop would look something like this.
[@ for (i = 0; i < (sizeof(myStr)/sizeof(int)) - 1; i++) @)
The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array, to find the
(type)(variable)
type: any variable type (e.g. int, float, byte)
variable: any variable or constant
int i; float f; f = 3.6; i = (int) f; // now i is 3
When casting from a float to an int, the value is truncated not rounded. So both (int) 3.2 and (int) 3.7 are 3.