Lab 3 Variables, Constants and Operators: 3.1 Objectives
Lab 3 Variables, Constants and Operators: 3.1 Objectives
We know from the previous classes that a variable is such an information which keeps on changing all
the time. In C++, variables serve the same purpose. Upon declaration of variable in a program, a
sufficient place become reserved in computer memory. This memory space is labeled with a specific
name chosen by programmer. The following program is introducing a variable myAge and printing its
value on output screen.
#include <iostream>
using namepace std;
int main()
{
int myAge = 18;
cout<<"My age is “ <<myAge<<“years”;
return 0;
}
Here myAge is an identifier for a variable and line# 5 is assigning a value 18 to this variable. This equal
sign is serving as an assignment operator. In other words we are defining a variable in this line.
While introducing a variable in a program you have observed that we also need to tell which type of
variable is this. Type is always being written before variable’s name as in the example below:
int myAge;
This statement tells the compiler to reserve a space in memory, labeled as myAge. And how much space
will be allocated? This depends on the type of variable of course.
Data Type Storage Size in memory
int (whole numbers) 4 bytes
char (characters) 1 byte
float (decimal numbers) 4 bytes
double (decimal numbers) 8 bytes
Note: Always choose an appropriate data type for a variable after identifying the nature of the value
going to be stored in it.
#include <iostream>
using namespace std;
int main()
{
float radius; //declaration of a variable
radius = 4.0;
cout << "PI = " << PI << endl;
cout << "Radius = " << radius << endl;
cout << "Circumference = " << 2 * PI * radius << endl;
return 0;
}
It is very clear form above example that the way we define a constant is almost same as the way a C++
variable is being defined. The difference is that constants are preceded (in C++) by the word const. You
can give any identifier (i.e. name) to them. Here “PI” is an identifier for a constant, which is of type
double. The value assigned to this constant is 3.14, which will remain fixed throughout the execution of
program.
Note: It is good programming practice to use named constants in your program to explain the meanings
of numeric values.
Activity 1:
Try changing the value of a constant later. Does it work? Does it give an error?
IDENTIFIERS
Identifiers are used to name variables, constants and many other components of a program. There are
following rules which we must need to follow to write an identifier:
They should consist exclusively of letters, digits and the underscore _ character
They cannot begin with a digit
They cannot duplicate reserved words used in C++ such as int or if
All characters in C++ are case sensitive; thus memory locations called simple, Simple, and
SIMPLE are three distinct locations
The following are some arithmetic operators used to perform different operations on
variables/constants. Most of them are easy to understand and use.
When we divide two integers, of course the result of this division will be an integer. This is because the
decimal part is truncated or “chopped” from the number. Thus 9/5 will give the answer 1 not 1.8. For
this reason there are two division operations for integer numbers. The modulus operator, (%) used only
with integers, gives the remainder of a division operation.
Therefore, 9 / 5 gives 1 while 9 % 5 gives 4 (the remainder of the division).
Activity 2:
Observe the output of following:
C++ EXPRESSIONS
We know that an assignment statement consists of two parts: a variable on the left and a value to be
assigned on the right. The right side may have an expression as well. An expression in C++ is a
combination of variables, constants and various operators. For example:
circumference = 2*PI*radius ;
diff = 105 - 78 ;
ans = 8 * 4/2 + 9 - 4/2 + 6 * (4+3);
Expressions are evaluated (i.e. simplified and converted to one value) and the result is placed in the
memory location corresponding to the variable on the left. Let’s consider the last expression. Can you
tell in which order it would be evaluated?
The answer depends on the precedence of the operators used in this expression.
Parenthesis
High to low
Unary operators
Multiplication, division, modulus
Addition, subtraction
To perform some other basic operations like “square root” and “power” in C++ we have the following in-
built functions (using them requires including the “cmath” library):
sqrt (number)
√9 would be written as sqrt(9)
pow (number, exponent)
23 would be written as pow(2,3)
TYPE CASTING
If you store a floating point number (i.e. a real number) into an int variable, you can lose information in
two ways:
• The fractional part is lost.
• The magnitude may be too large.
Yet, sometimes you do want to convert a floating-point value into an integer value. If you are prepared
to lose the fractional part and you know that this particular floating point number is not larger than the
largest possible integer, then you can turn off the warning by using a cast. A cast is a conversion from
one type (such as double) to another type (such as int). It is not safe in general, but it can be done when
you know it to be safe in particular circumstances.
You have very clear idea in this problem that the value stored in result would be 0. What if you need to
get exactly 0.20 in “result” variable? We can achieve this by type casting.
result = static_cast<float>(num)/den;
Activity 3:
Try the following cast and see what happens: