Arduino Data Type

From WikiOD


The data type in C refers to an extended system used to declare variables or functions of different types. The type of variable determines how much space it occupies in memory and how to interpret the stored bit pattern.

The following table provides all the data types you will use during Arduino programming.

void Boolean char Unsigned char byte int Unsigned int word
long Unsigned long short float double array String-char array String-object

void[edit | edit source]

The void keyword is only used in function declarations. It indicates that the function is not expected to return any information to the function that called it.

Example:[edit | edit source]

 Void Loop ( ) {
    // rest of the code
 }

Boolean[edit | edit source]

A Boolean value holds one of two values, true or false. Each Boolean variable occupies one byte of memory.

Example:[edit | edit source]

 boolean val = false ; // declaration of variable with type boolean and initialize it with false
 boolean state = true ; // declaration of variable with type boolean and initialize it with false

Char[edit | edit source]

A data type that occupies one byte of memory and stores a character value. The character literal is written in single quotes:'A', for multiple characters, the string uses double quotes: "ABC".

However, the characters are stored as numbers. You can view the specific encoding in the ASCII chart . This means that arithmetic operations can be performed on characters using ASCII values. For example, the value of'A'+1 is 66 because the ASCII value of the capital letter A is 65.

Example:[edit | edit source]

 Char chr_a = a ;//declaration of variable with type char and initialize it with character a
 Char chr_c = 97 ;//declaration of variable with type char and initialize it with character 97
[[File:Image.15.png|center|frameless|620x620px|alt=]]
Image.15.png

Unsigned char[edit | edit source]

Unsigned char is an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255.

Example:[edit | edit source]

 Unsigned Char chr_y = 121 ; // declaration of variable with type Unsigned char and initialize it with character y

Byte[edit | edit source]

One byte stores an 8-bit unsigned number from 0 to 255.

Example:[edit | edit source]

 byte m = 25 ;//declaration of variable with type byte and initialize it with 25

Int[edit | edit source]

Integer (int) is the main data type for digital storage. int stores a 16-bit (2-byte) value. This produces a range of -32768 to 32767 (minimum value is -2^15, maximum value is (2^15)-1).

The size of int varies from board to board. For example, in Arduino Due, int stores a 32-bit (4-byte) value. This produces a range of -2147483648 to 2147483647 (minimum value -2^31 and maximum value (2^31) -1).

Example:[edit | edit source]

 int counter = 32 ;// declaration of variable with type int and initialize it with 32

Unsigned int[edit | edit source]

unsigned int (unsigned integer) is the same as int and stores 2 bytes. However, they only store positive values, resulting in a valid range of 0 to 65535 (2^16)-1. Due stores 4-byte (32-bit) values, ranging from 0 to 4294967295 (2^32-1).

Example:[edit | edit source]

 Unsigned int counter = 60 ; // declaration of variable with 
    type unsigned int and initialize it with 60

Word[edit | edit source]

On Uno and other ATMEGA-based boards, a word stores a 16-bit unsigned number. On Due and Zero, it stores a 32-bit unsigned number.

Example:[edit | edit source]

 word w = 1000 ;//declaration of variable with type word and initialize it with 1000

Long[edit | edit source]

Long variable is an extended size variable used for digital storage, storing 32 bits (4 bytes), from -2147483648 to 2147483647.

Example:

Long velocity = 102346 ;//declaration of variable with type Long and initialize it with 102346
Unsigned long[edit | edit source]

The unsigned long variable is an extended size variable used for digital storage and stores 32 bits (4 bytes). Unlike the standard long, unsigned long does not store negative numbers, and their range is 0 to 4294967295 (2^32-1).

Example:[edit | edit source]

 Unsigned Long velocity = 101006 ;// declaration of variable with 
    type Unsigned Long and initialize it with 101006

Short[edit | edit source]

short is a 16-bit data type. On all Arduinos (based on ATMega and ARM), a short stores a 16-bit (2 byte) value. This produces a range of -32768 to 32767 (minimum value is -2^15, maximum value is (2^15)-1).

Example:[edit | edit source]

short val = 13 ;//declaration of variable with type short and initialize it with 13

Float[edit | edit source]

The data type of a floating point number is a number with a decimal point. Floating-point numbers are often used to approximate analog and continuous values ​​because their resolution is higher than that of integers.

The floating point number can be as large as 3.4028235E+38 or as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.

Example:[edit | edit source]

 float num = 1.352;//declaration of variable with type float and initialize it with 1.352

Double[edit | edit source]

On Uno and other ATMEGA-based boards, double-precision floating-point numbers occupy four bytes. In other words, the double implementation is exactly the same as the float, and there is no gain in accuracy. On Arduino Due, double has 8-byte (64-bit) precision.

Example:[edit | edit source]

 double num = 45.352 ;// declaration of variable with type double and initialize it with 45.352