Arduino

Reference Language | Libraries | Comparison | Changes

## sizeof

#### Description

The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.

#### Syntax

sizeof(variable)

#### Parameters

variable: any variable type or array (e.g. int, float, byte)

#### Example code

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.

char myStr[] = "this is a test";
int i;
void setup(){
Serial.begin(9600);
}
void loop() {
for (i = 0; i < sizeof(myStr) - 1; i++){
Serial.print(i, DEC);
Serial.print(" = ");
Serial.write(myStr[i]);
Serial.println();
}
delay(5000); // slow down the program
}
[Get Code]
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. Note also that a properly formatted string ends with the NULL symbol, which has ASCII value 0.

    for (i = 0; i < (sizeof(myInts)/sizeof(int)) - 1; i++) {
      // do something with myInts[i]
    }

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

### Share

### This link has expired.
Please re-subscribe to our Newsletters.

### Subscribe to our Newsletters

Email

Please enter a valid email to subscribe

Arduino Newsletter
Arduino Store Newsletter

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy

###### Newsletter

- ©2015 Arduino
- Copyright Notice
- Contact us
- About us
- Careers