## Arduino Data TypesThe data types are used to identify the types of data and the associated functions for handling the data. It is used for declaring functions and variables, which determines the bit pattern and the storage space. The data types that we will use in the Arduino are listed below: - void Data Type
- int Data Type
- Char Data Type
- Float Data Type
- Double Data Type
- Unsigned int Data Type
- short Data Type
- long Data Type
- Unsigned long Data Type
- byte data type
- word data type
## void Data TypeThe void data type specifies the empty set of values and only used to declare the functions. It is used as the return type for the functions that do not return any value. Let's understand with an example in Arduino. Consider the below code. ## Int Data TypeThe integer data types are the whole numbers like 5, -6, 10, -123, etc. They do not have any fractional part. The integer data types are represented by The size of int is 2 bytes ( 16 bits). Minimal range: -32768 to 32767 or - (2^ 15) to ((2 ^ 15) - 1) In the ATmega and Arduino UNO boards, the int data types store the value of 2 bytes. On the boards like Arduino Zero and MKR1000 (SAMD boards), and Arduino Due, the int data type stores the value of 4 bytes or 32 bits. The Minimal range in such case would be - (2^ 31) to ((2 ^ 31) - 1) or -2,147,483,648 to 2,147,483,647. The negative numbers are stored in the form of 2's complement, where the sign bit or the highest bit is flagged as the negative number. The syntax is used as: where,
For example, Any variable or identifier becomes an integer variable and can hold only integer values. Let's understand with an example in Arduino. Consider the below code. ## Char Data TypeThe char datatype can store any number of character set. An identifier declared as the char becomes a character variable. The literals are written inside a single quote. The char type is often said to be an integer type. It is because, symbols, letters, etc., are represented in memory by associated number codes and that are only integers. The size of character data type is For example, character ' A ' has the ASCII value of 65. If we specify, ' A ' + 2, it will have the ASCII value of 67. The syntax is: where,
Let's understand with an example. Consider the below code. The ASCII table is shown below: ## Float Data TypeA number having the fractional part and a decimal part is considered as a floating-point number. For example, 4.567 is a floating-point number. The number 13 is an integer, while 13.0 is a floating-point number. Due to their greater resolution, fractional numbers are used to approximate the contiguous and analog values. Floating point numbers can also be written in the exponent form. The numbers can be as large as 3.4028235E+38 and as small as -3.4028235E+38. The size of float data types is 4 bytes or 32 bits. The syntax is: where,
Let's understand with an example. Consider the below code. ## Note: We need to add a decimal point to a number. Otherwise, it will be considered as an integer. For example, 14.0 is considered as a float number, while 14 is an integer.The floating-point numbers can also be converted to integers. For example, ## Double Data TypeThe double data type is also used for handling the decimal or floating-point numbers. It occupies twice as much memory as float. It stores floating point numbers with larger precision and range. It stands for double precision floating point numbers. It occupies 4 bytes in ATmega and UNO boards, while 8 bytes on Arduino Due. The syntax is: where,
## Unsigned int Data TypeThe unsigned int stores the value upto 2 bytes or 16 bits. It stores only positive values. The range of unsigned int data type is from 0 to 65,535 or 0 to ((2 ^ 16) - 1). Arduino Due stores the unsigned data value of 4 bytes or 32-bits. The difference between Unsigned and signed data type is the sign bit. The int type in Arduino is the signed int. In a 16-bit number, 15 bits are interpreted with the 2's complement, while the high bit is interpreted as the positive or negative number. If the high bit is '1', it is considered as a negative number. The syntax is: where,
For example, ## short Data TypeThe short is an integer data type that stores two bytes or 16-bit of data. The range of short data types is from -32768 to 32767 or - (2^ 15) to ((2 ^ 15) - 1). The ARM and ATmega based Arduino's usually stores the data value of 2 bytes. The syntax is: where,
For example, ## long Data TypeThe long data types are considered as the extended size variables, which store 4 bytes (32 -bits). The size ranges from -2,147,483,648 to 2,147,483,647. While using integer numbers, at least one of the numbers should be followed by L, which forces the number to be a long data type. The syntax is: where,
For example, ## Unsigned long Data TypeThe unsigned long data types are also considered as the extended size variables, which store 4 bytes (32 -bits). It does not store negative numbers like other unsigned data types, which makes their size ranges from 0 to 4,294,967,295 or (2^32 - 1). The syntax is: where,
For example ## byte1 byte = 8 bits. It is considered as an unsigned number, which stores values from 0 to 255. The syntax is: where,
For example, ## wordIt is considered as an unsigned number of 16 bits or 2 bytes, which stores values from 0 to 65535. The syntax is: where,
For example, Next TopicArduino Variables |