OldSchoolCode

.com



Variables
Now that you know how to print text, you will learn the basics of writing flexible code. The first step in flexible code is understanding variables. A variable is nothing but a reserved space in memory for the CPU to place temporary data. C has different kinds of variables, but in the end they all resolve to the same thing: an address in memory.

The reason there are different kinds of variables is to tell the compiler the context you wish to use the data. Different types of variables reserve different amounts of memory and have different amounts of numerical precision, for example: An 'int' and 'float' will reserve 4 bytes of memory, while a 'char' will only reserve 1 byte of memory. So what can you do with these variables? In the next tutorial we'll show you how to use them to store and modify user input. For this tutorial we'll show you how to set, modify and print them directly in your program.

To declare a variable you put the variable type and then a name you can use to reference it. Here are some variable types declared in code:
//a whole number between -2,147,483,646 and 2,147,483,647
int myInt;

//a floating point number (a number with decimal places)
//between -2,147,483,646 and 2,147,483,647
float myFloat;

//a character (like a letter on the keyboard)
char myChar;

The first step is to declare the variables and set them to default values. You can set the default values right when you declare the variables by using the = (equal) sign.
void main( void )
{
   //declare 2 integers
   int numberOfBoys  = 2;
   int numberOfGirls = 4;
   
   //declare 2 floating point numbers
   (you put the 'f' after the fraction number to tell
   the compiler it's a floating point number)
   float distanceToPhoenix   = 2543.40f;
   float distanceToWisconson = 1000.50f;
}

Now that our variables are declared we can use them in code to perform some mathematical operations (don't worry, this will be easy math!).
void main( void )
{
   //declare 3 integers
   int numberOfBoys  = 2;
   int numberOfGirls = 4;
   int totalNumberOfPeople;
   
   //declare 3 floating point numbers
   float distanceToPhoenix   = 2543.40f;
   float distanceToWisconson = 1000.50f;
   float distanceBetween;

   distanceBetween      = distanceToPhoenix - distanceToWisconson;
   totalNumberOfPeople  = numberOfBoys + numberOfGirls;
}

Cool! Now we have procedurally calculated 2 values (distanceBetween and totalNumberOfPeople) based on preexisting values in code. We can use our function printf to display the output to the user. To have printf print out a variable, we need to tell it what type of variable to expect and where to place it. We tell it the type by the % (percent) sign. Following that we use a 'd' for integers, 'f' for float and 'c' for characters. It's actually pretty easy, and the more you use it - the easier it is to keep straight.
void main( void )
{
   //declare 3 integers
   int numberOfBoys  = 2;
   int numberOfGirls = 4;
   int totalNumberOfPeople;
   
   //declare 3 floating point numbers
   float distanceToPhoenix   = 2543.40f;
   float distanceToWisconson = 1000.50f;
   float distanceBetween;

   distanceBetween      = distanceToPhoenix - distanceToWisconson;
   totalNumberOfPeople  = numberOfBoys + numberOfGirls;
   
   printf( "The total number of people is: %d.\n", 
					totalNumberOfPeople );
   printf( "The distance between is: %f.\n", 
					distanceBetween );
}
Ok, lets step through this realy quick. I'm telling the computer to print out "The total number of people is: " then there is a %d. That tells the computer to look at the second argument in the function (arguments are separated by commas and here the first argument is the string in quotes). The second argument is the variable "totalNumberOfPeople". By using %d we're saying print it out as an integer. On the next line, it's the same thing only with a %f. This is saying print it out as a floating point number.

That's all there is to it! I encourage you to write a few programs using different types of variables and see what kind of results you get. If you try and mix variables (add floats to ints, add ints to chars, etc.) the compiler will warn you either because the amount of memory reserved for each variable type is different or because the precision between variables might be different (an int can't have fractional numbers and a float can).