You are on page 1of 25

Memory Location in C


Internal Memory

Inside every computer is a component
called internal memory.
Computer’s 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 .

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

. numeric or textual).indicates what type of information the address will store (e. .Names made up by the programmer.allows the programmer to refer to the memory location elsewhere in the program. .g.Memory Location   Data Type . Identifier .

are memory locations whose values can change during runtime (when the program is running). Often named in uppercase letters.Types of Identifier  Variables . radius (variable) .  Constants .are memory locations whose content cannot be changed during program execution.  Example: PI (constant). Most memory locations are variables.

numbers. and the underscore character No punctuation marks.Rules In Naming Identifiers (variables)     Name must begin with a letter and contain only letters. 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  .

They cannot be used as identifiers in your program .C++ Keywords   Keywords are predefined reserved words or reserved identifiers that have special meanings.

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 type of a memory location is determined by the programmer when declaring the location.Data Types    Memory locations come in different types and sizes A memory location will only accept an item that matches its data type.

Fundamental Data Types in C++        short int float double bool char string (user-defined data type) .

store integers (numbers without a decimal place)   Differences are range of values and memory used (int has the greater of both) .stores Boolean values (true or 1 and false or 0)  short and int .Fundamental Data Types in C++ bool .

stores characters ( real numbers (numbers with a decimal place)   Differences are range of values.Fundamental Data Types in C+ +  float and double . and memory used (double has the greater of each) char . symbol. or number that will not be used in a calculation)  Only one character stored at a time . precision.

is a user-defined data type. can store zero (blank) or more characters .Fundamental Data Types in C+ +  string .

Fundamental Data Types in C+ + .

string lastname. int width. . float length.  Examples: string age.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 .

.  // Displays 3 .e.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. cout << rainfall.88. “chopped off” and discarded)  The value is not rounded int rainfall = 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. .

name).Reads in a string with no blanks string name. Reading in a String Statement . . getline(cin.. cin >> name.Reads in a string that may contain blanks string name.

  system( ) is used to run the DOS commands from the C++ cls is the clear screen command .system (“cls”) .

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 .