We present another set of concepts in the computer science series as they relate to Arduino.
Previously on "Adventures in Science," I covered Arduino programming syntax. Now, we take a look at how to store numbers and characters in "containers" known as variables and how to access that data.
Before digging into literals and variables, we have to understand data types. A data type is a classification of information that tells the compiler how the programmer intends to use the information. In C, there are only three fundamental data types: integer (
int), floating point (
float) and character (
char). However, you will also sometimes see
void to indicate "nothing" or "no type."
Arduino supports more data types, such as
long int, which is an integer stored in 4 bytes, and
unsigned int to mean an integer that's only positive or 0.
Literals are fixed values that do not change throughout the program. For example, if you write the number
500 in your program, that's a literal. Literals for characters can be expressed between single quotes as any ASCII-encoded character, such as
Variables work like containers with labels. You can store information with the specified data type in a variable and then refer to the label later in the code when you need to retrieve or change the data. This can be extremely handy for manipulating data later in the code (with, for example, arithmetic operators) or setting a constant value once in the code (e.g., setting
led = 13; and then using
led instead of writing
13 several times).
Like your tutorials in written form? Here are a couple of guides that go over the basics of data types in Arduino and the American Standard Code for Information Interchange (ASCII):
Question for all you programmer types out there: Which do you prefer --- strongly typed languages or weakly typed languages? Why? Please respond in the comments below.