Slide02-Variables and Data Types
Slide02-Variables and Data Types
David Botwe
TYPES
1
WHAT IS A VARIABLE?
Variables are temporary storage locations where
data can be stored while a program is running.
Technology
Regent University College of Science &
Their values can be changed at any point over the
course of a program
2
CREATING VARIABLES
To create a variable, declare its name
and the type of information that it will
store.
Technology
Regent University College of Science &
The type is listed first, followed by the
name.
Example: a variable that stores an integer
representing the score on an exam could
be declared as follows:
int score ;
type variable-name;
3
CREATING VARIABLES (CONTD)
Now you have the variable (score), you will
want to assign a value to it.
Technology
Regent University College of Science &
Example: a score in the class exam is 98.
score = 98;
Technology
Regent University College of Science &
Letters (upper and lowercase)
Digits
A name cannot start with a digit
Underscores
A name should not normally start with an underscore
Technology
Regent University College of Science &
int, return, float, double
6
FUNDAMENTAL C++ DATA TYPES
C++ has a large number of fundamental or built-
in data types
Technology
Regent University College of Science &
The fundamental data types fall into three
categories
Integer data types
Floating-point data types
Character data types
7
INTEGER DATA TYPES
The basic integer data type is int
The size of an int depends on the machine
and the compiler
Technology
Regent University College of Science &
On PCs it is normally 16 or 32 bits
Other integer data types
short: typically uses less bits
long: typically uses more bits
Differenttypes allow programmers to use
resources more efficiently
Standard arithmetic and relational
operations are available for these types 8
INTEGER CONSTANTS
Integer constants are positive or negative whole
numbers
Technology
Regent University College of Science &
Examples
97
40000L L or l indicates long
integer
50000
23a (illegal)
9
CHARACTER DATA TYPES
Character data type char is related to the
integer types
Technology
Regent University College of Science &
Characters are encoded using a scheme
where an integer represents a particular
character
ASCII is the dominant encoding scheme
Examples
' ' encoded as 32 '+' encoded as 43
'A' encoded as 65 'Z' encoded as 90
'a' encoded as 97 'z' encoded as 122
10
CHARACTER CONSTANTS
Technology
Regent University College of Science &
Special
characters - delineated by a
backslash \
Two character sequences (escape codes)
Some important special escape codes
\t denotes a tab \n denotes a new line
\\ denotes a backslash \' denotes a single quote
Technology
Regent University College of Science &
Fractional part
Technology
Regent University College of Science &
precision floating point
value
Standard scientific notation
1.45E6 L or l indicates long double
0.979e-3L floating point value
13
EXAMPLE
/* Program to find the area of a circle*/
#include <iostream>
Technology
Regent University College of Science &
using namespace std;
int main()
{
float pi=3.142F;
float area, radius;
cout<<"Enter radius: ";
cin>>radius;
area= pi * radius * radius;
cout<<"Area = "<< area<<endl;
system("PAUSE");
return 0; 14
}
CONSTANT DEFINITIONS
Modifier const indicates that a variable cannot
be changed
i.e. Variable is read-only
Technology
Regent University College of Science &
Useful when defining variables representing
physical and mathematical constants
const float Pi = 3.1415;
Technology
Regent University College of Science &
"We are even loonier than you
think"
"Rust never sleeps\n"
"Nilla is a Labrador Retriever"
Technology
Regent University College of Science &
a single object
Some definitions
string Name = "Joanne";
string DecimalPoint = ".";
string empty = "";
string copy = Name;
string Question = '?'; // illegal
17
EXERCISE
What is a variable?
How do you declare a variable in a
Technology
Regent University College of Science &
program?
Which of the following are valid variable
names?
i) Amount
ii) 6tally
iii) my*Name
iv) salary
v) first Name 18
EXERCISE (CONT'D)
Technology
Regent University College of Science &
i) Population of Ghana
ii) Approximation of
iii) Average mark of a student
iv) First letter of your name
19