Reference.Sizeof History

Hide minor edits - Show changes to markup

October 20, 2012, at 05:14 PM by Scott Fitzgerald -
Changed line 21 from:

[@

to:
Changed line 38 from:

@]

to:
April 14, 2012, at 08:27 PM by David Cuartielles -
Changed lines 33-34 from:
    Serial.println(myStr[i], BYTE);
to:
    Serial.write(myStr[i]);
    Serial.println();
Added line 36:
  delay(5000); // slow down the program
Deleted line 37:
Changed lines 40-41 from:
to:

Note also that a properly formatted string ends with the NULL symbol, which has ASCII value 0.

Deleted line 46:
May 29, 2007, at 05:32 AM by David A. Mellis - sizeof int doesn't work, it must be sizeof(int)
Changed lines 9-13 from:

sizeof (variable)

sizeof variable

Both forms are accepted by the compiler

to:

sizeof(variable)

April 16, 2007, at 06:54 PM by Paul Badger -
Deleted line 40:
April 16, 2007, at 06:54 PM by Paul Badger -
Deleted lines 49-51:
April 16, 2007, at 08:56 AM by David A. Mellis -
Changed lines 44-47 from:

[@ for (i = 0; i < (sizeof(myStr)/sizeof(int)) - 1; i++) @)

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

April 16, 2007, at 06:29 AM by Paul Badger -
Changed lines 20-21 from:

This program prints out the text string a character at a time. Try changing the text phrase.

to:

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.

April 16, 2007, at 06:26 AM by Paul Badger -
Changed lines 5-6 from:

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

to:

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

Changed lines 9-18 from:

(type)(variable)

Parameters:

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

variable: any variable or constant

Example

to:

sizeof (variable)

sizeof variable

Both forms are accepted by the compiler

Parameters

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

Example code

This program prints out the text string a character at a time. Try changing the text phrase.

Added line 23:

char myStr[] = "this is a test";

Changed lines 25-28 from:

float f;

f = 3.6; i = (int) f; // now i is 3

to:

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);
  }

}

Changed lines 40-43 from:

Note

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.

to:

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++) @)

April 16, 2007, at 05:37 AM by Paul Badger -
Added lines 1-31:

sizeof

Description

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

Syntax

(type)(variable)

Parameters:

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

variable: any variable or constant

Example

int i;
float f;

f = 3.6; 
i = (int) f; // now i is 3

Note

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.

Reference Home

Share