[Tutorial][Community]2.Level 1-2, variables

To do anything interesting in programming you have to store data.
But data comes in all types and sizes, and we need to consider this, especially on a microcontroller like what the Pokitto is using.
The Pokitto has 36 kilo bytes of RAM where our variables will be stored.

That’s 36 864 bytes. A byte is 8 bits, and a bit is 1 or 0.

We have several options of variables.
Integer types:

  • bool is 8 bits, but can contain only the values true or false (1 or 0)
  • char is 8 bits, giving us a value of -128 to 127
  • short is 16 bits, giving a value of –32 768 to 32 767
  • int is 32 bits, giving a value of –2 147 483 648 to 2 147 483 647
  • long is 32 bits, giving a value of –2 147 483 648 to 2 147 483 647
  • long long is 64 bits, giving a value of –9 223 372 036 854 775 808 to 9 223 372 036 854 775 807
    As you can see short, int, long and long long have negative numbers. But what if we want to conserve space, and are not planning to use negative numbers? This is where the unsigned keyword comes in.
    If you add unsigned to the front of a variable declaration, all the negative numbers will be transferred to extra positive numbers: char becomes a value from 0 to 255, short becomes a value from 0 to 65 535, int becomes a value from 0 to 4 294 967 295, etc.
    It is usually best to use the smallest type you need, to conserve memory and cram the most content into your project.

If you need a variable like 3.14159, you want a floating point variable, which is basically an integer value with decimals:

  • float is 32 bits
  • double is 64 bits

The last basic type of variables is the pointer, which takes 32 bits of memory. Pointers store locations of data in memory; this will be covered in later tutorials.

Okay, that’s all fine and dandy, but how do you use these?
Well, let’s make an example: I’m going to move our text cursor around the screen, and since I don’t want text off the screen, I don’t need negative numbers. Because of this, I can use the unsigned keyword!
Since the screen is 220x176 in the high resolution mode, I think I will store the position in a char.
Inside the main function of our hello world we made in the last tutorial, we will add
unsigned char myVariable = 0;
You can change the variable name to whatever you like and assign a number to it; make sure to name it something that makes sense to you so you will know what it is!
Now I will replace the second parameter of the setCursor function, which will be the Y position on the screen:
After that I will increment myVariable by 1; this can be done in a couple of ways:

myVariable = myVariable + 1;
myVariable += 1;
myVariable ++;

The first one is a fairly basic definition, and the other 2 are short hand versions to kind of clean up the code.

If you run this now the text will race off the screen, but when myVariable hits 255, it will go back to 0 because of overflow.


This was a really good intro to a topic that is often not explained well enough. Thanks!

Actually, bool is 8 bits and has the values true or false

yea i know but i want to explain that the type bool is just a number

i would love to go into architecture details and that the smallest data is a byte instead of a bit or nibble, but it think that flies over people heads a little also true and false are just definitions of 1 and 0

But it’s better to use true and false now. You should get people used to using them now, instead of confusing them later when you have to point out that it’s better to use them instead of 1 and 0.

And people shouldn’t be thinking of bool as being just a number. It should be considered a variable type that holds a binary state.


sure but how do you explain that without TMI that there heads explode
the concern is more on its a binary state thats stored in a large container due to architecture, and idk how im going to explain bitshifting compressed booleans and stuff like that wich migth come up since memory is at a premium, also idk if the compile has the byte keyword since i have not even explained that char is going to print out in ascii

You say:
bool is 8 bits but can contain only the values true or false

Presumably, you’ll provide more detail and examples on the use of bool types later on, at which time you will point out that true and false actually evaluate to 1 and 0.

ok changed it, hope this is good enough to explain it later

1 Like

With a properly written program, you should never have to explain that true is actually 1 and false is actually 0. You should never care.

You just need to explain that if, while, etc. statements evaluate for true or false with bool types.

For assignment and type conversions, you should evaluate and assign:

my_char = my_bool ? 1 : 0;

The compiler will optimise this down to

my_char = my_bool;

but the programmer doesn’t need to think about this.


This is getting to be level 100-2.

Lets not frighten everybody, OK? :wink:

1 Like

hmm well i work the other way around, knowing that if just checks that is 0 or not 0

if people don’t know how they memory works they will just end up copy past c++ code from online and everything will be like ints and doubles due to the standard conventions which is going to be slower and bigger wen its hardly needed

there has to be some understanding of the hardware, especially on micros

From my experience with the Gamebuino, I have seen some pretty nice first games made by people who wrote fairly inefficient code, and that was on a much less capable chip than the Pokitto!

I think that it’s more important that somebody learns how to start coding first before they can start optimizing. Too many details can be overwhelming, and optimization is something that somebody can learn along the way after they learn the basics. Plus, packing multiple booleans into a single byte sounds like almost the definition of “premature optimization” to me. Somebody shouldn’t be doing that until they have experience and they’re to the point that every byte of ram matters. So I think for beginner tutorials, these details aren’t needed.

1 Like

fair point, but atleast having some notion of memory is alright, basicly what we coverd so far is enugh to get most people by for a very long time
i could just have said here you get bools, char, short and floats you can solve most biginner stuff with that

this is now a wiki post, you can edit it, add to it

1 Like

Nice job explaining that! However, I think you missed the unsigned keyword in front of char myVariable = 0;, so I’ll fix that now :slight_smile:

well also have to explain what unsigned keyword changes
would you say it removes negative numbers and doubles the max size or how would you explain it properly?

I updated it a bit…I gave some examples of the range of values for unsigned variables. Also, I have now realized just how annoying it is to type the grave symbol; I keep hitting tab :stuck_out_tongue:

This looks pretty complex for beginners. I wrote a series of tutorials for the Arduboy and I just explained variables as ways for the Arduboy to remember values (as int’s) and then introduced bool’s. Logically, that makes most sense to me. Not sure why a noob would care that a bool is 8 bits. Like, when would they even use that info at such an early stage?

1 Like

I don’t honestly feel like rewriting these tutorials right now, but you have a very good point there. I’m planning to make a Pokitto Wikia next Friday, and make my own tutorial there, so I’ll follow your advice when I write it.

People only leurn once. Better give them the info they need at the start. Changing peoples habit for an int=number is hard
And optimizing types is essential for micro consoles