Data Types: One size does not fit all
Learn Programming and Electronics with Arduino - A podcast by Programming Electronics Academy
Categorie:
Discussion: We're going to explore some of the specific data types you'll encounter as you begin to program with Arduino. We’ll tackle data types in more detail, to include: Integer and Integer Qualifier Long Float Byte Boolean Character Data Types Recap Let's review what we’ve learned in previous units. We know that when we create a variable, we need to declare its data type. We also know that a data type is simply a way to classify the types of information that you're going to put in a variable. Data type determines several things: what range of values can be stored in the variable how much space and memory that variable needs what types of operations can be performed on that variable Integer We'll begin our discussion of data types with the most common one that you'll see in this course - the integer. This is abbreviated “int” when you type it into the Arduino IDE. The integer is really the go-to data type if you need to store a number. It can hold a value from -32,768 all the way up to 32,767. Keep in mind that it only holds a whole number. You can't store a decimal, such as 3.14, in a variable whose data type is integer. Also, an integer is a 16-bit value. In other words, it takes 16 bits (or two bytes) of memory to store an integer. It doesn't matter if you put a small number in that integer variable like 10, or a big number like 30,000. The compiler will always set aside two bytes for any integer, regardless of number size. In the grand scheme of things, that's pretty small and is why integers are our friends. They don't take up that much space, even if you need to hold a very large value. Now, what happens if you try to put a number bigger than 32,767 inside an integer variable? Well, it actually rolls over. I’m sure most of you are familiar with Pac-Man. You know how if he goes off the screen, he comes right back on the other side? The same thing happens with variables. Let’s say we had an integer variable storing 32,767. Then, we add one to it. The result will actually be -32,768, which is called rolling over. Likewise, if you subtract one from -32,768, the result will be 32,767. This leads us to a really important point. When you create a variable, you should have some idea about how big you expect the stored value will be. That expected value will help you determine what data type you should use for a given variable. Remember, the key aspect of variables is that they change - they can vary. At some point, you'll probably do calculations with those variables and change what is stored there based on your results. You can only put so much water in a bucket before it spills over. Therefore, if the value in that variable at any time exceeds the size allow by its data type, it's going to roll over. You can imagine that might return some really weird results. With this in mind, what happens if you need to store a number bigger than 32,767? Well, you have a couple of options. However, before we abandon the integer for a data type that takes up more of our precious memory storage, let's talk about what signed and unsigned means. Integer Qualifier By default, an integer is signed. This means that it accepts both positive and negative signs. So when we say signed, we're talking about the fact that the value could be negative or positive. What if we know we won’t be working with negative numbers? Our variable will never need to store negative values. For example, if I'm using a variable to store the number of days that have passed, there's never going to be a negative number of days. The number is only going to increase from its starting point - unless, for all of you 80’s kids out there, I jump in a Delorean or something like that. If this is the case, we can use what is called a qualifier to make an unsigned integer. This gets rid of all the negative values and shifts them into the positive range. To use the unsigned qualifier, all you have to do is write unsigned before the “int” data type and voila... now you can store a value in the range from zero to 65,535 AND you’re still only using two bytes of storage. Totally awesome! Rolling over still works the same way, though. If you add one to 65,535, you will roll back to zero. Long That’s great, but what if you need a number bigger than 65,535? This brings us to a long. Long is a data type that stores four bytes of data and can hold a value from -2,147,483,648 to 2,147,483,647. You can think of a long as a really big integer. It acts the same way in that it can only hold whole numbers and can be made to qualify as unsigned if needed. This reaps you a whopping range of zero all the way up to four million and some change. Float We still keep running into this whole number wall, though. What if I want to store a decimal value? In programming, a number with a decimal point is called a floating point number. The point can float around the number. The data type for a floating point number is simply the float, and it can hold a value in the range from 340 undecillion 282 decillion, 315 nonillion (or 3.4028235E38 for you math whizzes). That's pretty big, so you might be thinking, “Geez, how much memory does it take to store a floating point number?” It's actually only a cool four bytes. That's right. It's no bigger than the long. How is this possible? It comes down to precision. In reality, a floating point number is only good up to six or seven actual numbers that mean anything. That's the total number - not just how many numbers after the decimal point. A number like 4,056.897 would be within the precision of a floating point number. You can see we only have seven digits there. The same would go for something like 40.1234. The less significant numbers after that, though, are going to be rounded off, chopped off, and battered. You really don't know exactly how the compiler's going to handle them. Large floating point numbers can be made using a capital “E” following the number. This means the same as it did in math class. For example, 42E2 means 42 times 10 to the second, or 4,200. You can think of the number that follows the E as the number of zeroes you want to add to the number before the E. To be honest, I hardly ever use the E. I just want to throw it out there just in case you run into it. Byte Another data type is the byte. A byte can store a number from zero to 255. As the name implies, it only takes up one byte of storage. That’s extremely small. You could also run into a number like B10010 being set equal to a byte. This is a number represented in the binary numeral system. B is the binary formatter. It lets the computer know to interpret the number following the B as a binary number. We aren’t going to delve into the binary numeral system because we really don't need to right now. You can check out the further reading section if you want to learn more about that, though. Boolean There is a data type that goes even smaller than a byte. The Boolean data type only takes up one bit of data. It can only hold one of two values: either true or false. Boolean is quite interesting. What happens if you try to put the number 2 in a Boolean data type? Does it roll over? No, it doesn't. With Boolean, any value other than zero is true. Numbers like 2, 5, -2, and 47 put in as a Boolean variable would be represented as true. False can be represented by several things. It can be represented by the keyword FALSE, the number zero, or the keyword LOW. We'll talk more about LOW later on. The true state can be represented by the keyword TRUE, any non-zero number as illustrated previously, and the keyword HIGH. If a variable only needs to represent two states, then a Boolean data type is a good option. Character This brings us to the final data type, the character. A character data type takes one byte of storage, and it holds a character symbol, such as a letter. The letter is actually stored as a number, and that number represents a specific text character in a system called ASCII. ASCII stands for the American Standard Code for Information Interchange. It's the way text is encoded numerically in programming. You can assign values to the character data type two different ways. It can be set equal to the actual character you want, as in an actual a, n, asterisk, or dollar sign, but you have to put single quotes around that character. The other way is to assign it as a number value since characters can be represented by numbers. For example, the following two lines of code would be equivalent because 97 is a lowercase a in ASCII code. This also means that you can perform math on characters. We won’t get into that now, but it's kind of neat. Review Let's review what we've covered in this lesson. We talked a little bit more about data types and what they do for us. We talked about integers and the fact that they only hold whole numbers. Roll over and integer qualifiers were also explained, with special attention given to unsigned qualifiers. Long data types were next and how it's a really big number. Then, we moved on to the float for floating point numbers and the byte for simple numbers from zero to 255. The last two data types were Boolean, which is either true or false, and character. I hope you don’t feel overwhelmed with all of this information. You don't have to memorize everything. I just wanted at the very least to give you a feel for what they represent because I guarantee you're going to see them as you open up different programs. Take a deep breath, soak it all in, and I’ll see you in the next lesson.