# Data Types and Variable

When programming, we always work with data, either processing them by doing math, or moving them around. Most of the time programming job requires that we do both.

For example, if we write a pong game, we first need to ‘draw’ a ball, imagine that a ball is made of lots of small dots (we usually can them pixels), we’ll need some math to determine where each dot needs to be drawn at. Then we want to move the ball around,  we’ll probably just move the pixel data around.

So what is data?

In the simplest form, let’s think data as ‘number’.

Even for just number, there are different ‘types’ of them. For example, integer, fractional, there is also the difference between positive and negative.

The data needs to be stored somewhere, thus the size of the data matters. If we know the number is going to be small, we do not want to reserve large storage space for it. On the other hand, if the number can grow to be really big, we need to make sure we have enough space for it. For example, in the pong game above, we probably only need a small number to record the level the player is at, but would need a big number to record player’s current score.

To manage all different types of data, C language defines data types, some of the basic types are,

• char: character, range from -128 to 127
• short: short integer, range from -32768 to 32767
• int: integer, range from -32768 to 32767, or -2,147,483,648 to 2,147,483,647 (we call this platform dependent, which we’ll learn later, for now it is safe to assume int has a range of the latter)
• long: long integer, range from -2,147,483,648 to 2,147,483,647

Each of the above integer types also has a corresponding ‘unsigned’ type, thus,

• unsigned char: from 0 to 256
• unsigned short: from 0 to 65536
• unsigned int: from 0 to  4, 294,967,296
• unsigned long: from 0 to  4, 294,967,296

For fractional (or floating point) number,

• float: 6 decimal places
• double: 16 decimal places
• long double:19 decimal places

I’ll leave the actual range of these types for your research, at this point it is enough to know that these are good for fractional math, such as 3.14 * 0.96.

• void: special type to indicate no data available.

As mentioned above, data needs to be stored somewhere, we call that storage ‘variable’,

• variable: the name to the storage place that holds the data

To define a ‘variable’, we use the following syntax,

<type> <variable name>

Once the variable is defined, we can then store data there, please keep in mind that the data stored must match the variable type.

Here’s a simple data type example,

A few things to note,

1. variable can only be accessed after they are defined.
2. variable can be used to store data, and storing data must be on the right hand. In other words, the following would be incorrect,
234567 = iy;
3. variable only holds one copy of data, old data gets lost when new data is stored. For example, add the following in line #12, and see what happens, ix = 999;
4. printf can print out formatted data, “%d” and “%f” above are format specifier. Research yourself to see what more printf can do.
5. note line #4 and line #8, they are there to help us and others to understand what the program does. They’ll be ignored by the compiler. It is always a good habit to write comments when programming. C language comments can be wrapped around by /* and */.

So much for the data types and variables, let’s move on …