Programming I
(PRP-211)
Data Types and Variables
Presented By:
Michael R Chinguwo
Computing and Information Technology Department
The Polytechnic
Introduction
In Programming Language, you need to use various variables to store
various information.
Variables are nothing but reserved memory locations to store values .
This means that when you create a variable you reserve some space in
memory.
Based on the data type of a variable, the operating system allocates
memory and decides what can be stored in the reserved memory.
C++ offer the programmer a rich assortment of built-in as well as user
defined data types.
Introduction (Cont’d)
The data we use every day is not of the same type.
Some data values are numbers, others are letters etc.
How you manipulate these values is also different. For numbers you
can add them using a plus sign (+). But this is not so for letters.
Even numbers are not the same. Others are fractions while others are
whole numbers.
So in programming there is a need to know the type of the data values
to be used in a program.
Data Types
Following lists are some basic C++ data types:
Integers (int) – for positive and negative whole numbers
Float (float) – for real numbers (floating numbers)
Double (double) - for real numbers (double precision floating
numbers)
Character (char) – for any characters
Boolean (bool) - for true or false
Valueless (void) -Represents the absence of type.
Wide character (wchar_t ) - A wide character type.
Data Types (Cont’d)
Several of the basic types can be modified using one or more of these
type modifiers:
signed
unsigned
short
long
Data Types (Cont’d)
Basic fundamental data types in C++
Data Types (Cont’d)
The size of variables might be different from those shown in the above
table, depending on the compiler and the computer you are using.
Data Types (Cont’d)
This code will produce correct size of various data types on your computer.
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
Data Types (Cont’d)
This example uses endl, which inserts a new-line character after every
line.
<< operator is being used to pass multiple values out to the screen.
Using sizeof() function to get size of various data types.
Note:
When the code is compiled and executed, it produces the result which
can vary from machine to machine.
Variables
Variable is a location of a memory identified by a name whose content
can change.
A variable is a container which hold values in programming.
Each variable (and its content) is accessed by a name.
The name of the variable is also called identifier
A variable has
name
value and
address.
Variables (Cont’d)
Variable Definition
Is the place where the variable is created
Is allocated storage.
Variable Declaration
Is the place where nature (type) of variable is stated, but no space is
allocated.
Variable Initialization
Means assigning a value to the variable.
Variables (Cont’d)
Variables can be created many times, but defined only once.
Memory space is not allocated for a variable while declaration, it
happens only on variable definition.
Variable Declaration
To put a variable into existence is called variable declaration
Variable declaration is requesting OS to prepare a part of memory to
be used for storing program data.
A variable is declared as follows:
data_type variable_name;
For example;
int age;
This declares a variable of data type integer whose identifier/name is
age
Variable Declaration (Cont’d)
You can declare variables of the same data types in one statement as
shown below
In general:
data_type variable_name1, variable_name2;
For example;
int height, mass;
This declares variables of data type integer whose identifiers/names
are height and mass
Initializing Variables
When variable is declared (created), it can now be assigned a value.
A variable can be assigned a value as:
variable_name = value; // assuming variable_name is
declared
For example,
age = 20;
You may use a single statement to declare the variable and assign a value to
it:
data_type variable_name = value;
This is called variable initialization
Initializing Variables (Cont’d)
For example;
float height = 1.62;
This declares variable of data type float whose identifier/name is
height and initialized to 1.62.
Initializing Variables (Cont’d)
Initializing a variable also called initiating a variable is giving the
variable an initial (a starting value).
This is usually done at declaration.
They are three ways to initialize variables at declaration in C++:
i. Using =
For example int age = 20;
ii. Using ()
For example int (20);
iii. Using {}
For example int {20};
Initializing Variables (Cont’d)
If you don’t explicitly initialize numeric data types such as int, float,
double, variable is implicitly initialized to zero (0)
For example:
int age; //same as int age = 0;
cout<<age;
Displays 0.
cin and variables
Mostly data assigned to variables is entered by users of the program
(at a time program is running and not during coding).
Remember, cout object, is for displaying content.
Syntax is:
cin>>variable_name;
For example:
cin>>age;
This statement will return a cursor for a user to enter a value. When
the user types the value and presses ENTER, the value entered is
stored in variable called age.
cin and variables
Example
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<“enter your age => ”;
cin>>age; // this line is getting age from a user
cout<<“Your age is ”<<age; //this line displays age entered
return 0;
}
Constants
Constant is a location of a memory identified by a name whose
content CANNOT change.
A constant is a variable with unchangeable content.
Used to keep content that should not change at any point in a program.
Others call it a constant variable
Constant Declaration
A constant can be declared in two ways in C++:
i. Using #define
• Placed soon after #include… directives.
• Syntax:
#define CONSTANT_NAME VALUE
e.g. #define HEIGHT 1.4
Constant Declaration (Cont’d)
ii. Using keyword const
• Placed at same location as variables i.e. after int main() {
• Declaration similar to that of variables. But begins with const
keyword
• Syntax:
const data_type constant_name = value;
e.g. const float HEIGHT = 1.4;
Note:
Constant names should always be in block letters to distinguish
them from variable names.
Constant (Cont’d)
#include<iostream>
#define PI 3.14 //First Constant using #define
using namespace std;
int main()
{
const float HEIGHT = 1.4; //Second Constant using const keyword
cout<<PI<<endl;
cout<<HEIGHT;
return 0;
}
Note: You cannot assign a value to a constant after declaration.
Rules for Naming Variables and Constants
Names of variables or constants should follow the rules below:
1. Name does not contain a space.
For example: Int my age; my age is invalid identifier.
Use underscore instead e.g. int my_age;
2. Name does not start with a number.
For example int 1st_number; 1st_number is invalid identifier.
Instead say int first_number; or int number1;
It must start with a letter or an underscore ( _ )
Rules for Naming Variables and Constants
(Cont’d)
3. Name must not be C++ keyword.
4. Names are case sensitive.
For example
my_age and My_Age are different variable names due to
difference in their cases (first one is in lowercase, the second
one, some are uppercases)
5. Variable name should be unique.
Variable Example
A program that add two numbers entered by the user:
#include<iostream>
using namespace std;
int main()
{
int first, second, sum;
cout<<“enter two integers to add\n”;
cin>>first>>second;
Variable Example (Cont’d)
sum=first+second;
/* Adding contents of first and second and
storing in sum */
cout<<“Sum of entered numbers=”<<sum<<“\n”;
return 0;
}
Exercise
Write a program to calculate sum, difference, product, division of two
numbers.
Write a program to find area and perimeter of a rectangle.
Write a program to calculate simple interest.
Write a program to convert temperature from Fahrenheit into Celsius
and from Celsius into Fahrenheit
END