cstutoringcenter.com logo
E-mail:Password:

Become a premier member today to gain access to exclusive
member benefits! Just $5.00 to join FOR LIFE!






<< Tutorial 1
Tutorial 3 >>

Variables

***Please register FREE to rate this tutorial***
Current rank: / 5 stars with 23 votes.


Our YouTube video for this tutorial:

Tutorial Topics:



Topics
Variable descriptions
Local variables
Constant variables


By it's definition, a variable is a place in memory that holds some information. Some information may be numbers, characters, strings or booleans.

There are numerous ways to use and declare variables in C++. Here is one way to declare them:

type varName;

where, in the above, type is the appropriate data type of the variable (i.e int, double, char etc...), varName is a useful name for your variable. This way will ONLY DECLARE IT and not give it any initial value. Be warned with this method as you may run into what is called garbage values (just random junk from memory. Don’t worry about that term for now).

This method below will declare the variable and initialize it to a given value:

type varName = initial_value;

where, in the above, type is the appropriate data type of the variable (i.e int, double, char etc...), varName is a useful name for your variable, and the initial value will vary when given the data type.

Here is a chart of the most used data types in C++:

Type Description Size
char Character or small integer.
Signed range: -128 to 127
Unsigned range: 0 to 255
1 byte
short Short Integer.
Signed: -32768 to 32767
Unsigned: 0 to 65535
2 bytes
int Integer.
Signed: -2147483648 to 2147483647
Unsigned: 0 to 4294967295
4 bytes
long Long Integer.
Signed: -2147483648 to 2147483647
Unsigned: 0 to 4294967295
4 bytes
bool Boolean value. Either true or false.
Acceptable as 1 or 0 respectively.
1 byte
float Floating point number.
3.4e +/- 38 (7 digits)
4 bytes
double Double precision floating point number.
1.7e +/- 308 (15 digits)
8 bytes
string A special variable type holding successions
of characters between a pair of double quotes "".
(see tutorial 8 for more on strings).
----

Here is how we can declare and/or initialize some variables of different types in C++:

int age = 0;
     will declare an integer variable called age initialized 
     to the value 0.

char letter = 'A';
     will declare a char variable that will be initialized 
     to an uppercase A.

string name = "";
     will declare a variable called name of type string and 
     initialize it to nothing (a.k.a. the empty string).

float f;
     will simply just declare a float variable called f.

double trouble;
     will simply just declare a double variable called trouble.

Let's see an example program to see how to use and work with variables.

Example 1:
Name and age of user

This program will ask for the name and age of a user and output the values they enter.

#include <iostream>
#include <string>
using namespace std;

int main(){

     string name = "";
     int age = 0;

     cout << "Enter your first name: ";
     cin >> name;
     
     cout << "Now enter your age (be honest): ";
     cin >> age;

     cout << "Hello " << name << ". You are " 
          << age << " years old." << endl;

return 0;
}

When the above program is executed, the name and age of a user will be stored in their appropriate variables and a message will be displayed at the end.

A sample run of the program may look like:

Enter your first name: Joan
Now enter your age (be honest): 42
Hello Joan. You are 42 years old.

There are different ways that variables can be declared:

Local variables

Local variables are quite the opposite. They can be used in between two curly braces { } for a function. Once the closing brace appears, the variables life span is over. Below is a sample of this:

#include <iostream>
using namespace std;

int main(){

	int a = 3;
	char v = 'V';

	cout << a << v << endl;

return 0;
}

The integer a and character v are local to the main function. If you tried using them directly elsewhere, they would not exist.

Constant variables

Constant variables can be either local, static or global in the program. The restriction is that they have the same value each time, hence being constant. A common practice is to make the constant variable name all capital letters.

You cannot change the value of constant variable directly. So here is what something may look like:

#include <iostream>
using namespace std;

int main(){
	const double PI = 3.1415;
	PI = 4.5; //illegal, can't change.
return 0;
}

The above snippet would produce an error upon compiling. It will tell you that you cannot change the value of a constant variable. Take my advice: leave it alone.

Here is another snippet showing a constant:

#include <iostream>
using namespace std;

int main(){

    const int AREA = 25;
    int x = AREA * 5;

    cout << x << endl;

return 0;
}

<< Tutorial 1
Tutorial 3 >>