Lecture 4
Constants, Variables and
Data Types
1
1
Outline
Representations of Values
Variables
Identifiers
Declaring and Initializing Variables
Data Types
Type Compatibilities
2
Representation of Values
In understanding a problem to be solved,
there are data values to be represented
in the solution (program)
Specified data values that can be
determined from the problem
Data values that needs to be obtained,
stored, manipulated and processed
3
What are the Values?
PROGRAM Add Two Numbers
READ First
READ Second
Sum = First + Second
WRITE Sum
END PROGRAM
4
Example
“There are 10 students in a class. Results
of a test is to be listed in a report. In the
report, the class average is printed after
the student numbers, names, marks and
grades.”
5
Representations
Number of students - 10
Student numbers
Student’s names
Student’s marks
Student’s grades
Average mark
In programming languages, we use
variables and constants to represent
them 6
Variables
Variables are the representations we use
in programs to store the values to be
processed
For example
student_name
student_marks
When the program is running, the
computer allocates memory locations to
be used for the variables
7
Identifiers
Variables names are called identifiers
The data for the variable is stored at a
unique memory address
Identifier names allow us to symbolically
deal with the memory locations so that we
don’t have to deal directly with these
addresses
e.g. total instead of 1001110001110001
8
Identifiers
Memory Address
0000
0001
x is an integer variable 4 bytes
0002
int x; 0003
c1 and c2 are character variable 0005
char c1, 0007
char = 1 byte
c2; 0008
0009
f1 is a float variable 0010
float f1; 4 bytes 0011
0012
0013
0014
9
Identifiers
Choosing variable names
Use meaningful names that represent data to
be stored
First character must be
a letter (a-z, A-Z)
the underscore character ( _ )
Remaining characters must be
letters
Numbers (letter (0-9)
underscore character
10
Identifiers
Valid identifiers Invalid identifiers
a money$
a1 2names
student_name int
TRUE student number
_aSystemName
11
Reserved Words
In any programming language, there are
symbols to represent different components
of the language
Reserved words are keywords in C++
language, specified for usage in various
areas of the language
Usage will be explained as we look more
into elements of C++ language
Cannot be used as identifiers
12
C++ Keywords
13
Declaring Variables (Part 1)
Before use, variables must be declared
Tells the compiler the type of data to store
Examples: int number_of_boxes;
double one_weight, total_weight;
int is an abbreviation for integer.
could store 3, 102, 3211, -456, etc.
number_of_boxes is of type integer
double represents numbers with a fractional
component
could store 1.34, 4.0, -345.6, etc.
one_weight and total_weight are both of type double
14
Declaring Variables (Part 2)
Two locations for variable declarations
Immediately prior to use At the beginning (better)
int main() int main()
{ {
… int sum;
int sum; …
sum = score1 + score2; sum = score1 + score2;
… …
return 0; return 0;
} }
15
Declaring Variables (Part 3)
Declaration syntax:
Type_name Variable_1 , Variable_2, . . . ;
Declaration Examples:
float average, m_score, total_score;
double moon_distance;
int age, num_students;
int cars_waiting;
16
Initializing Variables
Declaring a variable does not give it a value
Giving a variable its first value is referred to initializing
the variable
Variables are initialized in assignment statements
double mpg; // declare the
variable
mpg = 26.3; // initialize the
variable
Declaration and initialization can be combined
using two methods
Method 1
double mpg = 26.3, area = 0.0 , volume;
Method 2
double mpg(26.3), area(0.0), volume; 17
Exercise
Declare and initialize two integers variables to
zero. The variables are named feet and inches.
Declare and initialize two variables, one int and
one double. Both should be initialized to the
appropriate form of 5.
Give good variable names for identifiers to store
the speed of an automobile
an hourly pay rate
the highest score on an exam
18
Data Types
A data type defines a set of values and operations
that can be applied to these values
When a variable is declared as a particular data
type, it defines what can be stored in the variable
and the operations that can be performed using
the variable
Example:
int x;
float y;
char a;
string name;
19
Numeric Data Types
2 and 2.0 are not the same number
A whole number such as 2 is of type int
A real number such as 2.0 is of type float or double
Numbers of type int are stored as exact values
Numbers of type float or double may be stored as
approximate values due to limitations on number
of significant digits that can be represented
20
Other Numeric Types
Various numeric types have different
memory requirements
More precision requires more bytes of memory
Very large numbers require more bytes of
memory
Very small numbers require more bytes of
memory
21
Numeric Data Types
Type Name Memory Used Size Range Precision
short 2 bytes -32,768 to NA
(short int) 32,767
int 4 bytes -2,147,483,648 NA
to 2,147,483,647
long 4 bytes -2,147,483,648 NA
(long int) to 2,147,483,647
float 4 bytes Approximately 7 digits
10-38 to 1038
double 8 bytes Approximately 15 digits
10-308 to 10308
long double 10 bytes Approximately 19 digits
10-4932 to 104932
22
Other Integer Types
long or long int (often 4 bytes)
Equivalent forms to declare very large integers
long big_total;
long int big_total;
short or short int (often 2 bytes)
Equivalent forms to declare smaller integers
short small_total;
short int small_total;
23
Integer Constants
Values stored in int variables do not contain
decimal points
Examples: 34 45 1 89
If specific values are written in the program,
they are called literal constants
Examples:
x = 34
x = 45 + 89
24
Floating Point Types
Numeric values with decimal points are
stored in these types of variables
float (often 4 bytes)
Declares floating point numbers with up to
7 significant digits
float not_so_big_number;
25
Floating Point Types
double (often 8 bytes)
Declares floating point numbers with up to
15 significant digits
double big_number;
long double (often 10 bytes)
Declares floating point numbers with up to
19 significant digits
long double very_big_number;
26
Floating Point Constants
Values stored in float or double variables
Simple form must include a decimal point
Examples: 34.1 23.0034 1.0 89.9
Floating Point Notation (Scientific Notation)
Examples: 3.41e1 (value is 34.1)
3.67e17 (value is 367000000000000000.0)
5.89e-6 (value is 0.00000589)
Number right of e (exponent) does not require a decimal
point
27
char Data Type
There are variables that store a single
character
char variables (short for character) are
usually 1 byte of memory
28
char Constants
Character constants are enclosed in single quotes
char letter = 'a’;
The values of the constants corresponds to the
ASCII character set
Special escape characters can be represented
with a backslash (\)
Examples: ‘\n’, ‘\0’, ‘\a’, ‘\b’, ’\t’, ‘\v’, ‘\r’, ‘\f’, ‘\’’, ‘\”’,
‘\\’, ‘\033’
29
The ASCII
Character
Set
30
string Data Type
Usually we store text values that are
sequences of characters e.g. name, ID,
address
string variables are used to store a
sequence of characters
To declare a string variable:
string name;
31
string Constants
A string of characters, even if only one character,
is enclosed in double quotes
A NULL value (‘\0’) is stored at the end of the
sequence to mark the end of the string
Examples:
“Hello” (containing Hello)
"a" (a string of characters containing character a)
Note the difference with 'a’ (character)
32
Boolean Data Type
As part of processing, C++ provides
variables that store true or false
bool variables are used, they contain only
true or false
To declare a variable of type bool :
bool isValid;
Note: true is stored as 1, false stored as 0
33
Named Constants
Similar to variable declaration with initialisation,
except there is a type qualifier const before the
declaration
Specifies that the value cannot be changed.
Example:
const float pi = 3.14159;
Note: the value must be assigned
34
Type Compatibilities
In general, we store values in variables of
the same type
Sometimes type mismatch happens
This is a type mismatch:
int int_variable;
int_variable = 2.99;
If your compiler allows this, int_variable will
most likely contain the value 2, not 2.99
35
int double (Part 1)
Variables of type double should not be
assigned to variables of type int
int int_variable;
double double_variable;
double_variable = 2.00;
int_variable = double_variable;
If allowed, int_variable contains 2, not 2.00
36
int double (Part 2)
Integer values can normally be stored in
variables of type double
double double_variable;
double_variable = 2;
double_variable will contain 2.0
37
char int
The following actions are possible but
generally not recommended!
It is possible to store char values in integer
variables
int value = 'A';
value will contain an integer representing 'A'
It is possible to store int values in char
variables
char letter = 65;
38
bool int
The following actions are possible but generally
not recommended!
Values of type bool can be assigned to int
variables
True is stored as 1
False is stored as 0
Values of type int can be assigned to bool
variables
Any non-zero integer is stored as true
Zero is stored as false
39
Summary
Based on the algorithm, determine the
representations to be used in the program
The variables are written in the program to
store values and used in the processing
The different data types of the variables
determine what could be stored and how
the variables could be used in the
processing
40
40
The End
41
References
“Starting Out with C++: From Control
Structures through Objects” by Tony
Gaddis, 9th Edition, Pearson Education,
2018
42
42