Lec04 Constants, Variables and Data Types
Lec04 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?
4
Example
5
Representations
Number of students - 10
Student numbers
Student’s names
Student’s marks
Student’s grades
Average mark
8
Identifiers
Memory Address
0000
0001
x is an integer variable 4 bytes
0002
int x; 0003
c1 and c2 are character variable 0005
0009
f1 is a float variable 0010
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
14
Declaring Variables (Part 2)
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.
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
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
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 small_total;
short int small_total;
23
Integer Constants
Values stored in int variables do not contain
decimal points
Examples: 34 45 1 89
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;
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
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;
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;
int int_variable;
double double_variable;
double_variable = 2.00;
int_variable = double_variable;
36
int double (Part 2)
Integer values can normally be stored in
variables of type double
double double_variable;
double_variable = 2;
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'
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