SlideShare a Scribd company logo
Introduction to Programming in C++
Eight Edition
Lesson 3.2:
Data types for memory
location
An Introduction to Programming with C++, Eight Edition
• Memory locations come in different types and sizes
• Type and size you choose depends on the item you want
to store
• 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
Selecting a Data Type for a Memory
Location
2
An Introduction to Programming with C++, Eight Edition
• Fundamental data types are basic data types built into
C++
– Also called primitive or built-in data types
– Include short, int, float, double, bool, and char
• bool data type stores Boolean values (true and false)
• short and int types store integers (numbers without a
decimal place)
– Differences are range of values and memory used (int
has the greater of both)
Selecting a Data Type for a Memory
Location (cont’d.)
3
An Introduction to Programming with C++, Eight Edition
• float and double types store real numbers (numbers
with a decimal place)
– Differences are range of values, precision, and memory
used (double has the greater of each)
• char type stores characters (letter, symbol, or number
that will not be used in a calculation)
– Only one character stored at a time
• string data type is a user-defined data type (defined with
a class, or group of instructions)
– Can store zero or more characters
Selecting a Data Type for a Memory
Location (cont’d.)
4
An Introduction to Programming with C++, Eight Edition
Selecting a Data Type for a Memory
Location (cont’d.)
5
An Introduction to Programming with C++, Eight Edition
Selecting a Data Type for a Memory
Location (cont’d.)
6
An Introduction to Programming with C++, Eight Edition
• Numbers represented in internal memory using binary
(base 2) number system (two digits, 0 and 1)
• We are used to the decimal (base 10) number system
(ten digits, 0 through 9)
• Character data is stored using ASCII codes
– Eight-bit codes (bit = binary digit, 0 or 1)
– Upper- and lowercase versions of letters have distinct
codes
• Computer distinguishes between numbers and ASCII
codes based on data type
How Data Is Stored in Internal Memory
7
An Introduction to Programming with C++, Eight Edition
How Data Is Stored in Internal Memory (cont’d.)
8
An Introduction to Programming with C++, Eight Edition
How Data Is Stored in Internal Memory (cont’d.)
9
An Introduction to Programming with C++, Eight Edition
How Data Is Stored in Internal
Memory (cont’d.)
10
An Introduction to Programming with C++, Eight Edition
• Setting an initial value for a variable or named constant
is called initializing
• Required for constants; recommended for variables
• Memory locations are usually initialized with a literal
constant (item of data that can appear in a program
instruction and be stored in memory)
• Data type of literal constant should match data type of
memory location it is assigned to
Selecting an Initial Value for a
Memory Location
11
An Introduction to Programming with C++, Eight Edition
• Numeric literal constants initialize short, int, float, and
double data types
– Can contain digits 0 through 9, +, -, ., and e or E
(for scientific notation)
• 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
Selecting an Initial Value for a
Memory Location (cont’d.)
12
An Introduction to Programming with C++, Eight Edition
• Before assigning initial value to a memory location,
computer checks that value’s data type matches
location’s data type
• If they don’t match, computer performs implicit type
conversion to match them
– If initial value is converted to type that holds
larger numbers, value is promoted
– If initial value is converted to type that only holds
smaller numbers, value is demoted
• Promoting will not usually have adverse effects, but
demoting can (information is lost)
Selecting an Initial Value for a
Memory Location (cont’d.)
13
An Introduction to Programming with C++, Eight Edition
• Important to initialize memory locations with values of
the same data type
• Named constants should be initialized with the value
they will hold for the duration of the program
• Variables whose initial values are not known should still
be initialized
– short and int types usually initialized to 0
– float and double types usually initialized to 0.0
– string types usually initialized to empty string (“”)
– bool types initialized to either true or false
Selecting an Initial Value for a
Memory Location (cont’d.)
14
An Introduction to Programming with C++, Eight Edition
Selecting an Initial Value for a
Memory Location (cont’d.)
15
An Introduction to Programming with C++, Eight Edition
• Variables and named constants are declared using a
statement (C++ instruction)
• A statement that declares a variable causes the
computer to set aside a memory location with the given
name, data type, and initial value
• Statements must follow correct syntax (rules of a
programming language)
• In C++, all statements must end with a semicolon
Declaring a Memory Location
16
An Introduction to Programming with C++, Eight Edition
When declaring variables, a data type and name must be
provided
• Syntax for declaring a variable in C++
– dataType variableName [= initialValue];
• After variable is declared, you use its name to refer to it
later in the program
• Initial value is optional but recommended
• If variable is not initialized, it contains the previous value
of that memory location, which may be the wrong type
(called a garbage value)
Declaring a Memory Location
(cont’d.)
17
An Introduction to Programming with C++, Eight Edition
• Syntax for declaring a named constant in C++ – const
dataType constantName = value; • The const keyword
indicates that the memory location is a named constant
(value cannot be changed during runtime) • Initial value
required for constants, unlike variables • As with
variables, after declaring a constant, you can use its name
to refer to it later in the program
Declaring a Memory Location
(cont’d.)
18
An Introduction to Programming with C++, Eight Edition
• Several advantages to using named constants when
appropriate
– Make program more self-documenting (meaningful
words in place of numbers)
– Value cannot be inadvertently changed during runtime
– Typing a name is less error-prone than a long number
– Mistyping a constant’s name will trigger a compiler
error; mistyping a number will not
– If the constant needs to be changed when modifying the
program, it only needs to be changed in one place
Declaring a Memory Location
(cont’d.)
19
An Introduction to Programming with C++, Eight Edition
Declaring a Memory Location
(cont’d.)
20
An Introduction to Programming with C++, Eight Edition
Declaring a Memory Location
(cont’d.)
21
An Introduction to Programming with C++, Eight Edition
Declaring a Memory Location
(cont’d.)
22
An Introduction to Programming with C++, Eight Edition
The End ♥ ♥ ♥
26

More Related Content

PPTX
Pointers in C Programming
PPT
358 33 powerpoint-slides_1-introduction-c_chapter-1
PPT
358 33 powerpoint-slides_3-pointers_chapter-3
PPTX
Functions in c language
PPTX
C Programming: Structure and Union
PPTX
Structure in C
PPT
Basics of C programming
Pointers in C Programming
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_3-pointers_chapter-3
Functions in c language
C Programming: Structure and Union
Structure in C
Basics of C programming

What's hot (20)

PPTX
Fundamentals of Python Programming
PPTX
Operator overloading
PPTX
Strings in C
PPT
01 c++ Intro.ppt
PPTX
Python Functions
PPTX
CPU Scheduling in OS Presentation
PPTX
Presentation on c structures
PPTX
Python
PDF
Compiler design error handling
PPT
C PROGRAMMING
PPT
Assembler design options
PPT
Input and output in C++
PPTX
File in C language
PPTX
Input-Buffering
PPTX
ER model to Relational model mapping
PPTX
Leaky Bucket & Tocken Bucket - Traffic shaping
PPT
Memory allocation in c
PPTX
File handling in Python
PPT
358 33 powerpoint-slides_2-functions_chapter-2
Fundamentals of Python Programming
Operator overloading
Strings in C
01 c++ Intro.ppt
Python Functions
CPU Scheduling in OS Presentation
Presentation on c structures
Python
Compiler design error handling
C PROGRAMMING
Assembler design options
Input and output in C++
File in C language
Input-Buffering
ER model to Relational model mapping
Leaky Bucket & Tocken Bucket - Traffic shaping
Memory allocation in c
File handling in Python
358 33 powerpoint-slides_2-functions_chapter-2
Ad

Similar to Lesson 3.2 data types for memory location (20)

PPT
Chapter 3 - Variables and Constants
PDF
Lesson 3.1 variables and constant
PDF
Lesson 4.1 completing the problem solving process
PPTX
introductiontothecomputerprogramming.pptx
PDF
variablesfinal-170820055428 data type results
PPTX
Variables in C++, data types in c++
PPT
programming week 2.ppt
PPT
Data Handling
PPTX
Programming using c++ tool
PDF
Basic Elements of C++
PDF
Lesson 5 .1 selection structure
PDF
Week 02_Development Environment of C++.pdf
PDF
C 20 for Programmers 3rd Edition P. Deitel
PPT
Programming of c++
PPTX
Chapter1.pptx
PPTX
Csc240 -lecture_4
PDF
C 20 for Programmers 3rd Edition P. Deitel
PDF
Get C 20 for Programmers 3rd Edition P. Deitel free all chapters
Chapter 3 - Variables and Constants
Lesson 3.1 variables and constant
Lesson 4.1 completing the problem solving process
introductiontothecomputerprogramming.pptx
variablesfinal-170820055428 data type results
Variables in C++, data types in c++
programming week 2.ppt
Data Handling
Programming using c++ tool
Basic Elements of C++
Lesson 5 .1 selection structure
Week 02_Development Environment of C++.pdf
C 20 for Programmers 3rd Edition P. Deitel
Programming of c++
Chapter1.pptx
Csc240 -lecture_4
C 20 for Programmers 3rd Edition P. Deitel
Get C 20 for Programmers 3rd Edition P. Deitel free all chapters
Ad

More from MLG College of Learning, Inc (20)

PPTX
PPTX
PC111-lesson1.pptx
PPTX
PC LEESOON 6.pptx
PPTX
PC 106 PPT-09.pptx
PPTX
PPTX
PPTX
PPTX
PC 106 Slide no.02
PPTX
PPTX
PPTX
PC 106 Slide 1.pptx
PDF
Db2 characteristics of db ms
PDF

Recently uploaded (20)

PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
master seminar digital applications in india
PDF
Pre independence Education in Inndia.pdf
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Business Ethics Teaching Materials for college
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Introduction-to-Social-Work-by-Leonora-Serafeca-De-Guzman-Group-2.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
master seminar digital applications in india
Pre independence Education in Inndia.pdf
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Renaissance Architecture: A Journey from Faith to Humanism
TR - Agricultural Crops Production NC III.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
GDM (1) (1).pptx small presentation for students
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Business Ethics Teaching Materials for college
Open Quiz Monsoon Mind Game Prelims.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Introduction-to-Social-Work-by-Leonora-Serafeca-De-Guzman-Group-2.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Lesson 3.2 data types for memory location

  • 1. Introduction to Programming in C++ Eight Edition Lesson 3.2: Data types for memory location
  • 2. An Introduction to Programming with C++, Eight Edition • Memory locations come in different types and sizes • Type and size you choose depends on the item you want to store • 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 Selecting a Data Type for a Memory Location 2
  • 3. An Introduction to Programming with C++, Eight Edition • Fundamental data types are basic data types built into C++ – Also called primitive or built-in data types – Include short, int, float, double, bool, and char • bool data type stores Boolean values (true and false) • short and int types store integers (numbers without a decimal place) – Differences are range of values and memory used (int has the greater of both) Selecting a Data Type for a Memory Location (cont’d.) 3
  • 4. An Introduction to Programming with C++, Eight Edition • float and double types store real numbers (numbers with a decimal place) – Differences are range of values, precision, and memory used (double has the greater of each) • char type stores characters (letter, symbol, or number that will not be used in a calculation) – Only one character stored at a time • string data type is a user-defined data type (defined with a class, or group of instructions) – Can store zero or more characters Selecting a Data Type for a Memory Location (cont’d.) 4
  • 5. An Introduction to Programming with C++, Eight Edition Selecting a Data Type for a Memory Location (cont’d.) 5
  • 6. An Introduction to Programming with C++, Eight Edition Selecting a Data Type for a Memory Location (cont’d.) 6
  • 7. An Introduction to Programming with C++, Eight Edition • Numbers represented in internal memory using binary (base 2) number system (two digits, 0 and 1) • We are used to the decimal (base 10) number system (ten digits, 0 through 9) • Character data is stored using ASCII codes – Eight-bit codes (bit = binary digit, 0 or 1) – Upper- and lowercase versions of letters have distinct codes • Computer distinguishes between numbers and ASCII codes based on data type How Data Is Stored in Internal Memory 7
  • 8. An Introduction to Programming with C++, Eight Edition How Data Is Stored in Internal Memory (cont’d.) 8
  • 9. An Introduction to Programming with C++, Eight Edition How Data Is Stored in Internal Memory (cont’d.) 9
  • 10. An Introduction to Programming with C++, Eight Edition How Data Is Stored in Internal Memory (cont’d.) 10
  • 11. An Introduction to Programming with C++, Eight Edition • Setting an initial value for a variable or named constant is called initializing • Required for constants; recommended for variables • Memory locations are usually initialized with a literal constant (item of data that can appear in a program instruction and be stored in memory) • Data type of literal constant should match data type of memory location it is assigned to Selecting an Initial Value for a Memory Location 11
  • 12. An Introduction to Programming with C++, Eight Edition • Numeric literal constants initialize short, int, float, and double data types – Can contain digits 0 through 9, +, -, ., and e or E (for scientific notation) • 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 Selecting an Initial Value for a Memory Location (cont’d.) 12
  • 13. An Introduction to Programming with C++, Eight Edition • Before assigning initial value to a memory location, computer checks that value’s data type matches location’s data type • If they don’t match, computer performs implicit type conversion to match them – If initial value is converted to type that holds larger numbers, value is promoted – If initial value is converted to type that only holds smaller numbers, value is demoted • Promoting will not usually have adverse effects, but demoting can (information is lost) Selecting an Initial Value for a Memory Location (cont’d.) 13
  • 14. An Introduction to Programming with C++, Eight Edition • Important to initialize memory locations with values of the same data type • Named constants should be initialized with the value they will hold for the duration of the program • Variables whose initial values are not known should still be initialized – short and int types usually initialized to 0 – float and double types usually initialized to 0.0 – string types usually initialized to empty string (“”) – bool types initialized to either true or false Selecting an Initial Value for a Memory Location (cont’d.) 14
  • 15. An Introduction to Programming with C++, Eight Edition Selecting an Initial Value for a Memory Location (cont’d.) 15
  • 16. An Introduction to Programming with C++, Eight Edition • Variables and named constants are declared using a statement (C++ instruction) • A statement that declares a variable causes the computer to set aside a memory location with the given name, data type, and initial value • Statements must follow correct syntax (rules of a programming language) • In C++, all statements must end with a semicolon Declaring a Memory Location 16
  • 17. An Introduction to Programming with C++, Eight Edition When declaring variables, a data type and name must be provided • Syntax for declaring a variable in C++ – dataType variableName [= initialValue]; • After variable is declared, you use its name to refer to it later in the program • Initial value is optional but recommended • If variable is not initialized, it contains the previous value of that memory location, which may be the wrong type (called a garbage value) Declaring a Memory Location (cont’d.) 17
  • 18. An Introduction to Programming with C++, Eight Edition • Syntax for declaring a named constant in C++ – const dataType constantName = value; • The const keyword indicates that the memory location is a named constant (value cannot be changed during runtime) • Initial value required for constants, unlike variables • As with variables, after declaring a constant, you can use its name to refer to it later in the program Declaring a Memory Location (cont’d.) 18
  • 19. An Introduction to Programming with C++, Eight Edition • Several advantages to using named constants when appropriate – Make program more self-documenting (meaningful words in place of numbers) – Value cannot be inadvertently changed during runtime – Typing a name is less error-prone than a long number – Mistyping a constant’s name will trigger a compiler error; mistyping a number will not – If the constant needs to be changed when modifying the program, it only needs to be changed in one place Declaring a Memory Location (cont’d.) 19
  • 20. An Introduction to Programming with C++, Eight Edition Declaring a Memory Location (cont’d.) 20
  • 21. An Introduction to Programming with C++, Eight Edition Declaring a Memory Location (cont’d.) 21
  • 22. An Introduction to Programming with C++, Eight Edition Declaring a Memory Location (cont’d.) 22
  • 23. An Introduction to Programming with C++, Eight Edition The End ♥ ♥ ♥ 26