Lesson 5: Variables – Data Types!

Thought we were done talking about variables?  NOT!

Variables are an extremely important part of programming in general and we are going to spend just a wee bit more time discussing them.

In our last lesson, I provided a list of variable types.  I’m going to expand on that list here and provide some examples.

bool True or False
char 256 character values
unsigned short int Integer from 0 to 65,535
short int Integer from -32,768 to 32,767
unsigned long int Integer from 0 to 4,294,967,295
long int Integer from -2,147,483,648 to 2,147,483,647
unsigned long long Integer from 0 to 18,446,744,073,709,551,615
long long Integer from -9,223,372,036,854,775,808  to 9,223,372,036,854,775,807
int (16bit) Integer from -32,768 to 32,767
int (32bit) Integer from -2,147,483,648 to 2,147,483,647
unsigned int (16bit) Integer from 0 to 65,535
unsigned int (32bit) Integer from 0 to 4,294,967,295
float Floating point number from 1.2e-38 to 3.4e38
double Floating point number from 2.2e-308 to 1.8e308

Basically the Integer related variable types above are self explanatory.  If you’re going to use an Integer for something, pick the smallest type that it fits within.

Example:  If you’re going to be returning an integer that’s always between 1 and 10, an unsigned short int would be the most efficient.  No need to use a variable that goes into the billions if you’re barely getting into double digit numbers.

Make sense? Sorta kinda? Good, moving on.

Using the bool type:

As mentioned above, bool is used for storing True or False.  These are both reserved C++ keywords.

The bool type is very useful for storing a setting that can be on or off, or anything else that exists in 1 of 2 states.


bool thisIsMySetting = false;

I know we haven’t gone over “if/else” statements yet but they’re pretty logical. What do you think will happen in the following code? Do you think we’ll print the hello world string or the “you’re wrong!” string?

int main()
    bool thisIsMySetting = false;

    if (thisIsMySetting == false)
        cout << "Hello Beautiful World of Programming" << endl;
        cout << "you're wrong!" << endl;

    return 0;

Answer:  In this particular case, we will print the hello world string.

Explanation: The “if” statement says “if this particular variable is equal to false, do the code indented under me.”  The “else” portion only gets ran when the “if” portion doesn’t get ran.  If we change “bool thisIsMySetting = true;” then the “else” statement would run and we would print “you’re wrong!”.

Using the char type:

The char type represents a single character, not an entire string, exactly one byte in size.  At least 8 bits.


char customerInput = 'Y';

If you look up the ASCII code for Y, you’ll see that it has a value of 89 in decimal.  When the code is built and ran, the compiler stores 89 in the memory space allocated for the customerInput variable.

Pretty strait forward.

Signed and Unsigned integers

Sign simply implies positive or negative.  Variable types with “unsigned” in front of them can contain only positive numbers.  Any of the other integer variable types (without “unsigned”) are technically signed and can contain positive and negative numbers.

I don’t want to get into the CS behind how these integers are assigned and stored in memory as that is a bit outside the scope of these tutorials.   If you’d like to learn the science behind how these integers are stored at the system level, I’d suggest googling “concepts of signed and unsigned integers.”  This should bring up some decent results that can explain it in more depth.

AAAAA OVERFLOW! You should avoid it.

We need to keep in mind that each data type can only contain a pre-determined amount of information.  If we exceed that limit, we have something called an overflow.  Think of an overflow like your car odometer hitting its max distance, like 99,999 miles, and rolling back over to 0 (depending on how old you are, you may not remember this beign a thing).

Code Example:  You have an unsigned short int which has a range of 0 to 65,535. If you increase the max by 1, the value overflows to 0!

Try testing out the following code in your IDE.  What value does your console display?

int main()
    unsigned short uShortValue = 65535;
    cout << "Incrementing unsigned short " << uShortValue << " by 5 gives: ";
    cout << ++uShortValue << endl;

    return 0;

Try messing around with it a bit more.  Use the table listed above and test out some different data types.  Maybe just a regular short instead of an unsigned?  What happens when you overflow that data types max value? Let me know in the comments below!

This issue can cause unpredictable events to occur in your programs so make sure you’re selecting the proper variable type as you get involved in more complex software.


Wait, there’s more variable information?  YOU BETCHA!

Floating-point: float and double

Floating point numbers are basically real numbers.  This means they can be negative or postivie and contain decimals.

float valueOfPi = 22.0 / 7;

A double precision float is defined the same way, just with the word double instead of float.

double valueOfPi = 22.0 / 7;

A double is just a larger floating integer.  It allows for more precise decimal values to be utilized.


Ok, I think that’s all I have on Variables and data types for now.   I’ll see all you braniacs in the next lesson!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s