Main Page. VBA, ASP, Visual Basic 6.0, and VB.net. Visual C++, C#, C++, C. HTML and maybe javascript. Forum,Games and Jokes, other stuff. Submit a Tutorial Today!

Section 2: variables.

    This section assumes that you are familiar with section 1: your first C++ program. If you have not read this section, then please read it before proceeding to this one.

VARIABLES: They're what you store information in.

    Basically, in this section, we will be looking at two things variables, used to store information, and inputting from the console. First, we need to know what a variable is. A variable is a block of memory in a program that stores bits of information (1 & 0's). A variable can only begin with a letter, but it can include numbers and underscores after the first character. It stores information either from files, the user, or information you need to use as a programmer for a program. A computer can hold many kinds of data. The built-in kinds or types of data that can be stored by C++ are listed below.

Table 2.1 - C++ Types.

Name of Type   What it holds.
char A single keyboard character.
short A number from -32678 to +32678.
int A whole number that can range from 1 byte to 2 bytes in length.
long A whole number that can range from -4 billion to positive 4 billion.
float A decimal number that can go to quite a small number. (16 bit)
double Even larger decimal number than the float. (32 bit)

(Note that eventually you will be creating new types. These are only the standard types.)

    Be aware that for different computers, the ranges for the values above can be changed. This is especially true for ints, that is why I will stay away from their use in the tutorial. Ok let's experiment with the use of variables some, so you can get the hang of using them in programs. Also you need to be aware that you can make any of these types be able to store only positive numbers by using the word unsigned in front of the type name. So instead of saying int a=0; you say unsigned int a=0;

#include "iostream.h"

int main()

{

    //This is a one line comment. You use it to make notes about programs.

    /* This is a multi-line comment. Use it if you need to make notes about programs that

    last more than one line. Be sure to end it with */

     short sendme=0;       //Declare a SHORT variable called sendme and assign it zero.

    cout << "The value of the variable is: " << sendme;     /*We chain together couts so we can output a string and then the variable. (This is legal.)*/

    return 0;

}

OUTPUT -

 The value of the variable is: 0

    The program just outputs the string and then it outputs the value that is in sendme, 0. Ok, now that we have seen that we can assign values to the variable and output it, let's see how two variables work together. Change the above program until it looks like the one below.

#include "iostream.h"

int main()

{

     short sendme=0,addme = 5;       //Declare a SHORT variable called sendme and assign it zero and a variable addme with value 5.

    cout << "The value of the variables are: " << sendme+addme;    /* Note how we added the variables together in the output statement. You could do this on a separate line and set it equal to the value of a third variable. */

    return 0;

}

    Remember algebra? A lot of the same rules apply here in C++. You use an operator (like +,-,x, or division) to modify a variable or constant (a value like 5). You basically use the operators like so.

var3 = var1 thentheoperator var2; or for a better example we can substitute result = value1 + value2;

    Always put the variable that holds the result on the left hand side and the two or more variables you do your operations with on the right hand side. You will get an error from the compiler otherwise. A table of operators follows below.

Table 2.2 - Simple Operators.

Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

       Modulus is when you take the whole number value of the remainder after you divide one number by another. It can range from 0 to one less than the divisor. Also, there are more operators than these; they will be discussed further right now.

    The other operators that the C++ language has are not like the first few, but they are related to them. They are used as shortcuts for operations performed often in C++. Also, others are used to manage the Binary Data in the variables. With them, you can shift the bits, add, or change the bits in a variable in various ways. The complex operators are listed below.

Table 2.3 - Complex Operators.

Operator (var indicates variable.) Purpose
var++; or ++var; Adds one to the variable either after (for the first case) or before (for the second case) an operation is done to it. It is called the increment operator.
var--; or --var; Same as increment operator except it subtracts one from the value of the variable. It is called the decrement operator.
var+=andsomevalue; Adds the value to the variable. Is the same as           var = var + andsomevalue;
var-=andsomevalue; Same but it subtracts that value.
var*=andsomevalue; Same but it multiplies the variable by that value.
var/=andsomevalue; Same but it divides the variable by that value.
var%=andsomevalue; Same as var = var % andsomevalue;
var | var2; Performs a binary OR between var and var2.
var & var2; Performs a binary AND between var and var
var >> 2; Shifts all the bits in var to the right two place holders.
var << 2; Shifts all the bits in var to the left two place holders.

<--Previous Section Go Back to C Area. Next Section-->
 

Binary Central by Anman: "http://www15.brinkster.com/anman/"