Description
The sizeof() function in Arduino is a built-in function that gives us the size of a variable or an array. The size of a variable is the total bytes needed to store the variable value, while the size of an array is the total bytes required to store all its elements.
Syntax
Here is the basic syntax for the sizeof() function in Arduino:
In this syntax, variable is the name of the variable or data type that you want to determine the size of. The sizeof() function gives the size of the variable in bytes as an integer value.
Parameters
This function takes following parameter:
variable: whose size we need to find.
The sizeof() function can take any variable or data type as its argument, including integers, floats, characters, arrays, and structures.
Returns
This function returns the total bytes in a variable or number of bytes taken by an array.
Note: It’s important to note that the sizeof() function does not include any additional overhead that may be required by the hardware or software. For example, if you have an array of 10 integers, the sizeof() function will return the size of the array in bytes, but it does not take into account any additional overhead that may be required by the hardware or software.
How to Find Length of String Using the sizeof() Function in Arduino
Following code takes a string and returns the length of it using the sizeof() function.
void setup() {
Serial.begin(9600);
}
void loop() {
for (byte i = 0; i < sizeof(myStr) - 1; i++) {
Serial.print(i, DEC);
Serial.print(" = ");
Serial.write(myStr[i]);
Serial.println();
}
delay(50000); // slow down the program
}
Above code starts by defining a character array called myStr that contains the string "Arduino“.
The setup() function started serial communication. Inside the loop() function, there is a for loop that iterates over each element of the myStr array using an index variable i.
The loop condition is subtracted from 1, which means that the loop will run from 0 to the length of the array minus 1. The reason for subtracting 1 is because the sizeof() function returns the total number of bytes required to store the array, which includes the null terminator character at the end of the string. Since we don’t want to include the null terminator in our loop, we subtract 1 from the length of the array.
The loop function will print out the index and corresponding character using the Serial.print and Serial.write functions. The program then delays for 50000 milliseconds before repeating the loop.
Output
In the given output below we can see the size of a string.
How to Find Array Size Using the sizeof() Function in Arduino
Here’s an example code that uses the sizeof() function to determine the length of an integer array:
void setup() {
Serial.begin(9600);
}
void loop() {
int arrayLength = sizeof(myArray) / sizeof(myArray[0]);
Serial.print("The length of the array is: ");
Serial.println(arrayLength);
delay(5000);
}
In this example, we have an integer array called myArray that contains the values 1 through 5. The setup() function initializes the serial communication, and the loop() function is where the main logic of the program resides.
Inside the loop() function, we use the sizeof() function to determine the total number of bytes required to store the myArray array. We divide this value by the number of bytes required to store a single element of the array (which is given by sizeof(myArray[0])) to get the length of the array. An integer variable named arrayLength will store this value.
We then use the Serial.print() and Serial.println() functions to print out the length of the array. Finally, we use the delay() function to pause the program for 5 seconds before running the loop again.
Output
In the given output below we can see the size of an array.
Conclusion
The sizeof() function in Arduino can determine the length of an array. By passing an array as an argument to the sizeof() function, you can easily obtain the number of elements in the array, regardless of its data type. It is important to note that sizeof() returns the size of the array in bytes. For further detailed description of this function read the code description in the above article.