Types and Memory

Every programming languages has variables.

A variable is something we can use to store data. This can be helpful in numerous ways.
For example, say we wish to write a program to count the number of vowels that appear in a sentence, an essential aspect of this program is a single variable, referred to as a counter which will increment once we come across a vowel. This counter is an example of an integer variable.
Furthermore, if we want to write a program that will calculate the average given a sequence of integers, we will once again need a variable to hold that average, considering that an average is rarely a whole number, we need a decimal number to hold this value. In C, we would use the float type.
Another useful data type (or variable type) is the char. It holds a single character.

In actual programming, declaring variables is very easy.

 int myInt;

This declares an integer named “myInt” when you declare a variable, you first state the type, than you give it a name. In C, there are some rules to follow when it comes to naming variables,

1. A Variable name can consist of any combination of alphabet letters, digits and underscores.
2. The first character of the variable name must either be letter or underscore. It should not start with the digit
3. No commas and blanks are allowed in the variable name
4. No special symbols other than underscore are allowed in the variable name
5. You can’t use specific keywords such as “do” “while” “if”, etc.

Similarly we can declare other variables:

 int x;
 char c;
 float _F;

Now we have an integer named x, a char named c, and a floating point variable named _F.

These variables are all stored in memory. The way memory works, in a simplified explanation is the following.


You can think of memory as a long street, with addresses. In the table above, I wrote the memory addresses as 0x1 -> 0x5 however actually, addresses are written as hexadecimal numbers. You may have heard the terms 32-bit and 64-bit operating systems.
What this actually refers to is the number of addresses available to reference. I.e. the largest address available for writing into.
Therefore, say you have a 1-bit operating system
this means you have 2^1 unique addresses, 0 and 1
2-bit addressing lets you talk about 4 unique addresses (00, 01, 10, and 11).
3-bit addressing lets you talk about 8 unique addresses (000, 001, 010, 011, 100, 101, 110, and 111).
A more common OS, the 32-bit, lets you address 2^32 unique addresses which is 4,294,967,296, or 4GB. That is why a 32-bit operating system cannot have more than 4GB of RAM.
Today, new computers run 64-bit operating systems, as you can imagine 2^64 is a huge number and essentially removes any address limitations allowing for 16.8 million terabytes of RAM.

Back to the example, as you can see.
X is stored in the address 0x1,
C is in 0x2 and
_F is at 0x3
Now a good question is, when we declare these variables what are their values?
When you declare a variable, and don’t initialize it, you have no way of knowing what it stores. It will simply take the value of whatever used to be at the address it’s assigned. This is just called “garbage”.
Initializing a variable means setting it to a specific value like so.

int q = 3;

This creates an integer variable named q and initializes it to 3.


It’s considered good programming style to always use constants in programs.
For example, if you want to write a program that calculates the area of a circle, avoid doing the following.

int Area;
int Radius;

Area = Radius * Radius * 3.14;

It’s good practice to declare Pi as a constant and use it like so:

const int PI = 3.14;

int Area;
int Radius;

Area = Radius * Radius * PI;

It’s also a common convention to name a constant with all capital letters.

In general, using constants in your program makes it more generic, allowing you to make changes in the future more easily.
Imagine you have the value 3.14 written for Pi in several different places in your project, which could be written across multiple files.
Now, say you decide that 2 decimal points is not precise enough and you wish to use 3.1415 as Pi instead.
You would have to track down every instance of 3.14 and adjust it accordingly.
If you have it declared once as a constant, all you need to do is update the declaration of your constant, and all instance will change accordingly.

Next Page: Preprocessors 

Leave a Reply


WordPress.com Logo


Google photo


Twitter picture


Facebook photo


Connecting to %s