You are on page 1of 25

Memory Location in C++

Internal Memory

Inside every computer is a component


called internal memory.
Computers internal memory is composed
of memory locations, each with a unique
numeric address.
Memory location is similar to collection of
storage bins.
Each address can store one item at a time.

Illustration of Storage Bins

Memory Location

Address or memory location can contain


numbers, text, or program instructions
To use a memory location, programmer
must reserve the address, called declaring.
Declaring a memory location is done with
an instruction that assigns a data type,
identifier and initial value (optional).

Memory Location

Data Type - indicates what type of


information the address will store (e.g.,
numeric or textual).
Identifier - allows the programmer to
refer to the memory location elsewhere
in the program.
- Names made up by the programmer.

Types of Identifier

Variables - are memory locations whose


values can change during runtime (when the
program is running). Most memory locations
are variables.

Constants - are memory locations whose


content cannot be changed during program
execution. Often named in uppercase letters.

Example: PI (constant), radius (variable)

Rules In Naming Identifiers


(variables)

Name must begin with a letter and contain


only letters, numbers, and the underscore
character
No punctuation marks, spaces, or other special
characters (such as $ or %) are allowed
Cannot be a keyword (word that has special
meaning in C++)
Names are case sensitive
Example: discount is different from DISCOUNT and
from Discount

Valid or Invalid Variables


totalSales
Total_Sal3s
Total Sales
total.Sales
4thQtrSales
totalSale$
T0taLsales

C++ Keywords

Keywords are predefined reserved


words or reserved identifiers that
have special meanings.
They cannot be used as identifiers in
your program

C++ Keywords
auto
const
double
float

break
continue
else
for

void
case
default
char

C++ Keywords
int
short
struct
endl

long
signed
unsigned
explicit

do
while
bool
string

Data Types

Memory locations come in different


types and sizes
A memory location will only accept an
item that matches its data type.
Data type of a memory location is
determined by the programmer when
declaring the location.

Fundamental Data Types in C++

short
int
float
double
bool
char
string (user-defined data type)

Fundamental Data Types in C++


bool - stores Boolean values (true
or 1 and false or 0)
short and int - store integers
(numbers without a decimal place)

Differences are range of values and memory


used (int has the greater of both)

Fundamental Data Types in C+


+

float and double - store real numbers


(numbers with a decimal place)

Differences are range of values, precision,


and memory used (double has the greater
of each)

char - stores characters (letter, symbol,


or number that will not be used in a
calculation)

Only one character stored at a time

Fundamental Data Types in C+


+

string - is a user-defined data type,


can store zero (blank) or more
characters

Fundamental Data Types in C+


+

Declaring Memory Location

When declaring a memory location a data type and


an identifier (variable or constant) must be provided
Syntax for declaring a variable in C++
DataType Identifier ;
Examples:
string age;
float length;
int width;
string lastname;

Assigning Floating-point Values


to Integer Variables

If a floating-point value is assigned to an


integer variable
The fractional part will be truncated (i.e.,
chopped off and discarded)
The value is not rounded
int rainfall = 3.88;
cout << rainfall;

// Displays 3

String Literals

Can store a series of characters in


consecutive memory locations.
"Hello"

Enclosed in a double quotation " "

Can define string data type in programs


string name;

char vs string literal constants

Character literal constants initialize char


data types

Consist of one character in single quotation


marks

String literal constants initialize string data


types

Zero or more characters enclosed in double


quotation marks
Empty string ( ) is a valid string literal
constant.

- Reads in a string with no blanks


string name;
cin >> name;

Reading in a String Statement

- Reads in a string that may contain blanks


string name;
getline(cin, name);

system (cls) ;

system( ) is used to run the DOS


commands from the C++
cls is the clear screen command

Knowledge Check
Identify the data type of the following variables:
Lastname
-PI where PI = 3.1416
-x where x = 100
-Radius where radius = 89
-True
-width where width = 10
-Age
-PhoneNumber
-0
-

Thank You!!

That in All Things,God May Be Glorifi

Prepared by: Prof. LRQ Natividad

You might also like