Arduino Data Type

From WikiOD
Revision as of 10:13, 26 August 2021 by Admin (talk | contribs) (→‎Boolean)


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

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

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

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

Example:

 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

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

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
Unsigned char

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

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

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

example

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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