# Data types¶

For those of you coming to this course frm Python, you are in for one significant shock. When you set up data containers (variables, for example), C++ requires that you fully define what kind of thing you want that container to hold, and you cannot change it during the program run. This is called strongly typed, meaning the compiler will work hard to make sure you are doing the right thing with each data item.

Each chunk of data requires some amount of memory that needs to be set up during the program run. We will attach a name to the memory so we do not need to worry about where it actually lives in memory. The compiler uses something called a symbol table that it builds as it processes your program. When you want to create a new container, you need to set up a data declaration:

## Data Declarations¶

These are simple to get started with, but we will see more to them later. For now, here is the declaration of a simple integer container:

int mydata = 10;


There are three parts to this declaration:

• The data type
• The container (variable) name
• An optional initial value

Here is the diagram for this:

### Available data types¶

The first part is called the data type, normally one built into the language. Common available types in C++ include:

Name Size Range
bool 8 (top 7 bits ignored) 0 or 1

char 8 -128 to 127 (signed) or 0-255 (unsigned

int 32 -2147483648 to 2147483647 (signed) or 0-4294967296 (unsigned)

float 32 see notes

There are other data types around. You need to consult your compiler reference to find these. If you expect to write code that is portable to other systems, do not use weird data types unless absolutely necessary, and document the details!

### Container name¶

This is a big part of good programming. You should choose names that help tell the program reader what the variable is for. Simple names like i, j, etc are often used as counters in loops for historical reasons, but other variables should be readable.

• “max_value” is far better than “x24”

There are conventions on how you write variable names. We will go over these when we discuss our style guide.

### Initial value¶

If you want to initialize a container, you can include that initial value on the declaration line (behind an equal sign). Leaving the initial value off might lead to problems later if you forget that the container always has some value in it, uninitialized containers just have an unknown value in them, and you risk running your program and using junk values. Your program might run, but it might not. You will not know until you try it.

A simple rule is sufficient to avoid problems:

Warning

Make the first reference to an uninitialized data item be an assignment operation that gives it a value!

## Scope¶

Once you start declaring your data containers, you also need to think about scope (no, not the mouth-wash!).

Scope defines where in your program a name can be used. There are a few basic rules you should follow

• Make sure to declare a name before you try to use it.
• Names outside of any function are global and available to all code below the declaration
• Names defined inside a function are local to that function
• C++ also lets you define names inside a block (a chunk of code inside curly braces. These are only available inside those braces.
• for-loop counter variables. These are available only inside the loop.