Main Gestures Programming Music Whoizzy? Links

C Programming

Part 1
Variables

A variable is a piece of your computers memory that you have control over. Like an object, it can have its own attributes, size, type, behavior . . . Variables are assigned duties (count, keep track) and values (1,2,3,a,b,c). They can then be manipulated. Here are the basic variable types, and what kind of information they can hold.

Type Description
int Integer values (-3, -2, -1, 0, 1, 2, 3 . . 1000)
float Floating point (decimal numbers) 1.3, 1.4, 399.3
char Characters, letters (a, b, c, d . . . A, B, C . . #, $, (, ), *)


The variable process

DECLARING/CREATING VARIABLES

When you declare a variable, you are assigning your program a piece of memory in which you have control over. You are making a claim to it. It's yours to do with it whatever you wish. It's like giving birth. This is how it is done.

int x;
float y;
char c;

This creates 3 variables (x, y, c) of 3 different types (int, float, char). Notice that NONE of them have the same name (x, y, c). No 2 variables, of any types, can have the same name. Your variable name is what you will use to access the computer memory opened up for you in the declaration. If 2 variables were to have the same name, then they would share the same memory. But this is not possible. When we cover pointers, you will find exceptions to this memory concept. But just remember, GIVE YOUR VARIABLES THEIR OWN UNIQUE NAME! This name is the doorway to the computers memory. Variables are your friend.

INITIALIZING VARIABLES

OK, so what. You put asside some memory by declaring a variable. Big deal. So what makes this so special. You need to give this variable some LIFE. You give it some life by initializing it. You make it unique.


/*Declaration (This is a comment, remember?)*/

int x;
float y;
char c;

/*Initialize*/
x = 1; 
y = 5.5;
c = 'z';

You just gave these variables life. x has the value of 1, y the value of 5.5 and c as the letter z. I introduced to you something new here. It is the assignment operator. You know . . that = sign. Caution!! The assignment operator is NOT to be used as it is mathematically!!! IT'S NOT EQUALITY!!!!

On the left side you have the variable name. On the right side you have the value you wish to 'pass into' the variable.

Before the line
x = 1;
x had a random number in it. Lets say 400. But when you wrote
x = 1;
x actually HELD that value of 1. You changed it!

PUTTING IT ALL TOGETHER


DOWNLOAD PROGRAM

#include <stdio.h>

int main(void)
{
	/*Declaration*/

	int x;
	int y;
	int z;
	
	/*initialize*/

	x = 5; /*x now holds the value of 5*/
	y = 3; /*y now holds the value of 3*/
	
	printf("The values after initialization. \n");
	printf("x = %d \n", x); /*prints x = 5*/
	printf("y = %d \n", y); /*prints y = 3*/

	/*Manipulate the variables*/
	
	x = y + 5; /*x is now 8*/ 
	y = x + 5; /*y is now 13*/

	printf("The values after manipulation. \n");
	printf("x = %d \n", x); /*prints x = 8*/
	printf("y = %d \n", y); /*prints y = 13*/

	/*Swap the values of x and y*/
	z = x;
	x = y;
	y = z;	
	
	printf("Values after swap. \n");
	printf("x = %d \n", x); /*prints x = 13*/
	printf("y = %d \n", y); /*prints y = 8*/
}
Here is a screen shot of the above code executing.

So what happend? Most of it is pretty straigt forward. For depth I will explain a couple of things though.

We declared 3 variables (x , y, z). Then initialized x to 5 and y to 3. We then manipulated these two variables. However we used each variable to manipulate the other!

Whatever is in x (5) is destroyed and replaced in the following manipulation. Because y has the value of 3, x becomes 8.

x = y + 5; /*x is now 8*/

So now x is 8. We then manipulated y, and the contents of y (3) are destroyed and replaced with the next manipulation.

y = x + 5; /*y is now 13*/

In the previous manipulaion, x was given the value of 8. Thus y now holds the value of 13.

Now for the swap. This was the fun part of the program. We put the contents in x into y, and the contents of y into x. However we had to use another variable z to help pass the values to eachother, because when you use the = operator, whatever is to the left is destroyed. We didn't want to loose the value prematurly in the first swap, so we had to pass off a value to z.

X = 8 and Y = 13

z = x; /*z = 8*/
x = y; /*x = 13 NO LONGER 8 which we want to make y equal to*/
y = z; /*y = 8*/

As you can tell, dealing with variables can be a sensitive issue and at times requires a bit of understanding as far as computer memory goes. Many times you will have to reinvent the wheel, even when just swapping 2 numbers.


NEXT LESSON

MAIN MENU

Found a broken link?
Mail Me