2 – Variables & Datatypes

If you went to school, you should actually already know what a variable is. Variables are used all over the place in math, even basic math. So this should be no new concept to you, with the exception being that we are applying this concept in a slightly different context.

Darn, I Forgot.

Alright, for those of you who need a quick refresher on variables; A variable is a friendly representation of a value, that has the potential of changing — hence the name a variable (varying value). An example of variables in math would be something like solving for the variable x in an equation (eg. 5 = x + 2).

Okey, Gotcha!

In programming, there are a few things we can do with variables; declaring, initializing/setting, reference, etc. Let’s go over these now:

In your template from the last lesson, replace the comment that says ‘insert code here’ with the following code:

// 1 - declare an integer variable naming it i
int i;

// 2 - initialize variable i to a value of 3;
i = 3;

// 3 - print an equation, using the variable 'i'
printf("5 = %d + 2", i);

 

EXERCISE 2.1
Just to remind you of the previous lesson, I want you to read the code example above, and identify the elements of it.

In the first line of code, we declare a variable of datatype ‘int’, and calling it ‘i’ — don’t worry about the datatype just yet, we’ll get to that later in this lesson. This means that the computer will allocate a chunk of memory with the size of an int and give it to you, which can then be set to a given value. Declaration of a variable does not give you any data to work with though, you have to give your variable a value in order to use it. In other words, whenever you declare a variable, the block of memory that has been allocated and given to you still holds the old data, that was in that memory before you allocated it — random data, leftovers from another application who used that block before you.

The second line of code initializes your newly created variable to a value of ‘3’. In other words, we are assigning actual data to our variable. If it gets you excited to know, you are actually writing your own data to the RAM of your computer — neat, huh?

On the third line we call the same function that we used in our ‘Hello World’ application, printing a formatted string, replacing the %d with our variable ‘i’. In this function, we reference our variable, meaning that in place of ‘i’ goes the data of the variable, in this case ‘3’ — again, don’t try to understand the function ‘printf’, only that we are using our variable in it.

Note that referencing a variable does not require a semi-colon, since referencing usually occurs within another instruction — such as a function-call, as done in this example. This is true for any instruction, should it be nested within another instruction.

EXERCISE 2.2
Declaration and initialization can be done in one line of code, instead of two. Try and see if you can figure out how on your own. I’ll be explaining this later in the this lesson, so don’t worry if you can’t.

 

Datatypes

A datatype denotes of what kind a variable is, I will go easy on you for now and introduce only 3 fundamental datatypes in this lesson. The objective of this part of the lesson is not necessarily to teach you of the individual datatypes, but rather to have you recognize datatypes and how to use them.

int
The int datatype represents integer values, that is any whole number (including negatives).

float
The float datatype represents floating point numbers, that is numbers that are not necessarily whole (eg. 1.2345).

char
This datatype represents a character value (eg. char theMagicalCharacter = 'a';) — this does not mean a string of characters, a char can only hold one character, we will get to do strings in a later lesson.

EXERCISE 2.3
You might have noticed in the char example shown above, that the value assigned to the char variable is enclosed by the ' sign. Why do you think that is?

The answer to exercise 2.3 is that character variable values could easily be mistaken for another variable. Like in our Hello World example, we had a variable called ‘i’, if you simply put char theMagicalCharacter = i; and didn’t enclose it, you’d be assigning the value of ‘i’ to the value of the char variable — so when assigning a character value to a char variable, we enclose the value with the ' sign.

EXERCISE 2.4
Using the printf function, from the previous lesson, alter the code to make it print a floating-point variable and a character variable. For this, you need to know that the %d stands for decimal (%d and %i are the same, %i of course stands for integer), %f stands for float and %c stands for character. The solution is provided below, have fun.

#include <stdio.h>

int main (int argc, char * argv[]) {
	
	// create and print an integer variable
	int integerVariable = 1;
	printf("an integer variable: %i\n", integerVariable);
	
	// create and print a float variable
	float floatVariable = 12.3456789;
	printf("a floating-point variable: %f\n", floatVariable);

	// create and print a character variable
	char characterVariable = 'a';
	printf("a character variable: %c\n", characterVariable);
	
	return 0;	// return succesful execution
}

Notice in the code example immediately above, we are declaring and initializing the variables in one line of code.

Summary

In this lesson you have learned how to declare, initialize and reference a variable. You also learned to recognize and use the various datatypes introduced by this lesson. More datatypes will be introduced throughout the lessons, as not all datatypes can be explained in such a simple context as printing its contained data and will be more understandable in applied example of its use.

I encourage you to play around with the code examples provided as you progress through the lessons, as learning programming is mostly a process of trial and error. Even though I try to explain things as simple as possible, programming is hard to learn and understand, especially if you have never seen any code before. But I assure you, once you get in and try these things on your own, you will get that ‘Oh, now I get it’ feeling — this is the reason I give you with a few exercises to work with. Just keep in mind, everything presented here is quite simple, it’s just a matter of getting your head wrapped around it — and that’s your job, young padawan.

.. or put it other words: “Neo, I can only show you the door. You are the one that has to walk through it.”.

Previous: Syntax
Next up: Operators

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s