Data Type
Before we can store data such as number, text, or even a function, we need to know what data type is. Data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. For example, if you want to store a number, you need to know if you want to store a whole number or a decimal number. If you want to store a text, you need to know if you want to store a single character or a sentence. If you want to store a function, you need to know if you want to store a function that returns a value or a function that doesn't return a value. In this section, we will learn about the most common data types in Arduino.
Integer
Integer is a whole number. It can be positive or negative. It can be as small as -32768 or as big as 32767. It is also called int. To declare an integer, you can use the following code:
int myInteger = 0;
You can also declare an integer without assigning a value to it. In this case, the value will be 0.
int myInteger;
You can also declare multiple integers in one line.
int myInteger1, myInteger2, myInteger3;
You can also declare multiple integers in one line and assign a value to them.
int myInteger1 = 0, myInteger2 = 0, myInteger3 = 0;
You can also declare multiple integers in one line and assign a value to some of them.
int myInteger1 = 0, myInteger2, myInteger3 = 0;
In this case, myInteger2 will be 0 and myInteger3 will be 0.
Different int type
In Arduino or C code you can define int type by using int8_t, int16_t, int32_t, int64_t. For example, int8_t is a 8-bit integer. It can be as small as -128 or as big as 127. It is also called byte. To declare a byte, you can use the following code:
int8_t myByte = 0;
for int16_t it is a 16-bit integer. It can be as small as -32768 or as big as 32767. It is also called int. To declare an int, you can use the following code:
int16_t myInt = 0;
for int32_t it is a 32-bit integer. It can be as small as -2147483648 or as big as 2147483647. It is also called long. To declare a long, you can use the following code:
int32_t myLong = 0;
for int64_t it is a 64-bit integer. It can be as small as -9223372036854775808 or as big as 9223372036854775807. It is also called long long. To declare a long long, you can use the following code:
int64_t myLongLong = 0;
There is also uint8_t, uint16_t, uint32_t, uint64_t. uint8_t is an unsigned 8-bit integer. It can be as small as 0 or as big as 255. It is also called unsigned byte. To declare an unsigned byte, you can use the following code:
uint8_t myUnsignedByte = 0;
for uint16_t it can be as small as 0 or as big as 65535. It is also called unsigned int. To declare an unsigned int, you can use the following code:
uint16_t myUnsignedInt = 0;
for uint32_t it can be as small as 0 or as big as 4294967295. It is also called unsigned long. To declare an unsigned long, you can use the following code:
uint32_t myUnsignedLong = 0;
for uint64_t it can be as small as 0 or as big as 18446744073709551615. It is also called unsigned long long. To declare an unsigned long long, you can use the following code:
uint64_t myUnsignedLongLong = 0;
Float
Float is a decimal number. It can be positive or negative. It can be as small as -3.4028235E+38 or as big as 3.4028235E+38. It is also called float. To declare a float, you can use the following code:
float myFloat = 0.0;
You can also declare a float without assigning a value to it. In this case, the value will be 0.0.
float myFloat;
You can also declare multiple floats in one line.
float myFloat1, myFloat2, myFloat3;
You can also declare multiple floats in one line and assign a value to them.
float myFloat1 = 0.0, myFloat2 = 0.0, myFloat3 = 0.0;
You can also declare multiple floats in one line and assign a value to some of them.
float myFloat1 = 0.0, myFloat2, myFloat3 = 0.0;
In this case, myFloat2 will be 0.0 and myFloat3 will be 0.0.
Char
Char is a single character. It can be a letter, a number, or a symbol. It is also called char. To declare a char, you can use the following code:
char myChar = 'a';
String
String is a sentence. It can be a word, a sentence, or a paragraph. It is also called String. To declare a String, you can use the following code:
String myString = "Hello World";
Boolean
Boolean is a true or false value. It is also called bool. To declare a boolean, you can use the following code:
bool myBoolean = true;
const
const is a keyword that is used to declare a constant. A constant is a value that cannot be changed. To declare a constant, you can use the following code:
const int myConstant = 0;
array
Array is a set of values. To declare an array, you can use the following code:
int myArray[10];
In this case, myArray is an array of 10 integers.
You can also declare an array without assigning a value to it. In this case, the value will be 0.
int myArray[10];
You can also declare an array with a value to it.
int myArray[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
You can change the value of an array by using the index. For example, if you want to change the value of the first element of the array, you can use the following code:
myArray[0] = 1;
Function
Function is a set of code that can be called by a name. It can return a value or not. To declare a function, you can use the following code:
void myFunction() {
// Your code here
}
In this case, myFunction is a function that doesn't return a value.
If you want to return a value, you can use the following code:
int myFunction() {
// Your code here
return 0;
}
In this case, myFunction is a function that returns an integer.
Lets say you want to return a float, you can use the following code:
float myFunction() {
// Your code here
return 0.0;
}
In this case, myFunction is a function that returns a float.