C++ Variable Types

Now we know the built in data types of C++ Programming Language. The next step in learning C++ is to learn how to declare, initialize and use variables of different types. For this purpose, create a new project, called “Variables Demo” following the instruction from the environment setup article. Add a file called “VariablesDemo.cpp” to your project.

Initially, we have to add following lines of code to the file:

//include a header file from Standard Library
#include 
using namespace std;
//the program starts from function called main
int main()
{
	 return 0;
}

As we can see, actually the main function is not doing anything. It is empty. Now we will print the values using variables.

Variable Definition

Definition of variable can be presented in the following way:

DataType variableName1, variableName2,…variableNameN;

The above line means “We have declared variables with names variableName1, variableName2,…variableNameN  that have the common data type DataType”

For better understanding declare some variables:

Declare single variable of type integer

int weight;

Declare 3 variables of type double

double price, height, length;

So, we can see that the declaration is basically very simple: we have to specify the type of your variable and after this write the name of your variable.

Variable Declaration

Variable declaration is used when you have multiples files and you want to use a variable which is defined in another file. To clarify thing the use of the following code:

extern int someVariable;

extern in above line tells the compiler that variable someVariable of type int exists somewhere. The compiler doesn’t know where does that variable is Defined and Initialized, it just knows that this variable exists.

Variable Initialization

Variable initialization is done by using operator “=”. Variable initialization means that you assign a value to this variable:

weight = 3; //Now weight is equal to 3

Above line of code assigns value 3 to the variable weight

In the same way we can initialize variables of type double:

price = 2.39;//price is set double value 2.39
length = 3.0;//length is 3.0

You can use any numeric value, mathematical operators and any other already initialized variables to initialize a variable. For example:

height = length * 2 + 0.3;

Now we can print the value of variables which we have created and initialized above:

cout << "Weight is " << weight << endl;
cout << "Price is " << price << endl;
cout << "Length is " << length << endl;
cout << "Height is " << height << endl;
cin.ignore();

We will get the following output:

C++ Variable Types

Arithmetic Operators in C++

The list of arithmetic operators in C++ are:

  • The + operator adds its operands.
  • The – operator subtracts the 2nd operand from the 1st.
  • The * operator multiplies its operands
  • The / operator divides its 1st operand by the 2nd.
  • The % operator finds the modulus of its 1st operand with respect to the 2nd.

What are Operands?

int i = 2 + 5;
int j = a - b;

In above code 2, 5, a and b are operands and ‘+’ and ‘-‘ are operators.

Let’s write a simple code to test arithmetic operators. Add the following lines of code before the line with cin.ignore():

cout << " 2 + 3 = " << 2 + 3 << endl;
cout << " 10 - 4 = " << 10 - 4 << endl;
cout << " 2.0 * 3.5 = " << 2.0 * 3.5 << endl;
cout << " 7.4 / 3. 2 =   "  << 7.4 / 3.2 << endl;
cout << " 100 % 7 = " << 100 % 7 << endl;

Add this code to the test variables demo and run the program. We should get the following output:

C++ Variable Types

To get a value for a variable from the user, we can use cin object from the iostream header. To get a value for the variable ‘weight’ we can use the following way:

cout << "Enter new value of weight" << endl;//shows this message to the user
cin >> weight;//get new value for weight from the console
cout << "New value for weight is " << weight << endl;//shows new value of the weight:
cin.ignore();

C++ Variable Types

Pay attention on the fact that when we want to output something we use cout << output; and when you want to get some input we have to use cin >> input; where input is a variable name.

Now you know the basic operations that can be performed on variables and you can use them in your programs. Try to write some simple programs that perform input and output of variables. Also try to perform some mathematical operations on the variables and print them.

Translate »