You are on page 1of 4

Riphah International University Faculty of

Computing Programming Fundamentals


Lab 2: Data type

Objective:
Learn basic data types in C++.
Learn how to use data type in problem
solving. Learn how to declare a variable

Variables (Introduction)

As a programmer, you have to decide what types of information are necessary for your program
and how this information would be used. When writing a program, you will provide these pieces of
information to the computer, the computer then puts them together. When your program runs it is
“loaded” into the memory (RAM). When the user is using your program, the information that your
application requests also goes into the RAM while your program is processing such requests.

Because your program will be made of many of these things, the computer needs to know what
these things would be, and how much space each one of them would need. Because such a thing can
change (vary) throughout your program, it is called a variable.

By its definition, a variable is a place in memory that holds some information. Some
information may be numbers, characters, strings or Booleans.

Declaration:

Every variable must be declared before use. or in simple words,


Before using a variable, you must first let the compiler know. Letting the compiler know about a
variable is referred to “Declaring” the variable. The compiler will need two pieces of information
concerning each variable: the amount of space the variable will need, and a name to recognize
that variable.
Therefore, the formula of declaring a variable is:
type VariableName;

Variable names:

When using the various necessary variables in your programs, each one of them need to be
identified. A variable is primarily recognized by its name. C++ provides rules for naming items
in a program.

The name of a variable:


Starts with an underscore “_” or a letter, lowercase or uppercase, such as a letter from a to z
or from A to Z. Examples are Name, gender, _Students, pRice.
Cannot include special characters such as !, %, ], or $
Cannot include an empty space
Cannot be any of the reserved words
Should not be longer than 32 characters (although allowed)

Programming Fundamentals Riphah International University


Lab 2: Data Types Faculty of Computing
2

A name can consist of one word such as country. A name could also be a combination of more than
one word, such as firstname or dateofbirth.

C++ is case-sensitive; this means that CASE, Case, case, and CaSe are four
completely different words.

All variable must have meaningful names.

Basic Data types in C

The following table shows the basic data types in C:

Data Type C Keyword Bytes Range Example


Data
Character char 1 -128 to 127 A
Integer int 4 -2,147,483,648 to 1
2,147,483,647
Floating Point float 4 -3.4E38 to 1.1
3.4 E38
Double precession double 8 -1.7e308 to 1.0000000008
floating point. 1.7e+308

The following example will print the actual size allocated based on your computer:
/* Displays the number of bytes used to store each basic type */
#include <iostream>

int main() {
cout<<"The size of char is \n"<<sizeof(char); cout<<"The
size of short is \n"<< sizeof(short); cout<<"The size of int is
\n"<<sizeof(int); cout<<"The size of long is
\n"<<sizeof(long); cout<<"The size of float is
\n"<<sizeof(float); cout<<"The size of double is
\n"<<sizeof(double); cout<<"The size of long double is
\n"<<sizeof(long double);

return 0;
}

Write the following segments of codes in Microsoft Visual C++. Execute and observe the output.
1. A program declares a single variable of type int.
#include
<iostream>
using
namespace std;
int main ()
3

{
int age; //declaration
cout<<"This program declares a variable age\n"<<age;
return 0;
}
2. Display a value of a variable
#include
<iostream>
using
namespace std;
int main ()
{
int age; //declaration
cout<<"This value of age is
“<<age; return 0;
}
You will observe that it will display some garbage value, the variable is uninitialized and its just
returns whatever value happened to be in that memory location.
3. Each variable must be initialized. Assignment operator (=) is used to store a value to
a variable.
#include
<iostream>
using
namespace std;
int main ()
{
int age = 21; //initialization
cout<<"This value of age is
“<<age; return 0;
}
Always initialize your variable when you define them. If you don't know what value a
variable should have when you define it, initialize it with zero.
4. Assignment operator (=) is also used to store result of a calculation in a variable.
#include <iostream>
using namespace std;
int main ()
{
int var1 = 10, var2 = 20;
int var3 = var1 + var2;
cout<<"This value of variable 3 is “<<var3;
return 0;
}
More than one variable can be declared in single statement (separated by comma)
5. Declare the variable with following names and observe which is correct and which not
the correct variable name is.
age-limit, _temprature, 3rdDay, lastName, mounth45

Laboratory Tasks:
1. Write a program that stores the integers 62 and 99 in variables, and stores the sum of these
two in a variable named total.

2. A customer in a store purchasing five items. The prices of the five items are :
Price of item 1 = 12.95
4

Price of item 2 = 24.95


Price of item 3 = 6.95
Price of item 4 = 14.95
Price of item 5 = 3.95

Write a program that holds the price of the five items in five variables. Display each item`s
price, the sub total of the sale. The amount of sales tax and the total. Assume the sales tax is
6%.

3. What is wrong with the following


program #include<iostream>
Using namespace
std; Int main(){

number = 62.7
double number;
cout<<number<<endl;

return 0;
}

You might also like