SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Computational Thinking
Computational ThinkingComputational Thinking
Computational Thinkingshowslidedump
 
Coal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly ProgrammingCoal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly ProgrammingMuhammad Taqi Hassan Bukhari
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Digital Electronics Notes
Digital Electronics Notes Digital Electronics Notes
Digital Electronics Notes Srikrishna Thota
 
Computational Thinking: Why It is Important for All Students
Computational Thinking: Why It is Important for All StudentsComputational Thinking: Why It is Important for All Students
Computational Thinking: Why It is Important for All StudentsNAFCareerAcads
 
Introduction to digital computers and Number systems.pptx
Introduction to digital computers and Number systems.pptxIntroduction to digital computers and Number systems.pptx
Introduction to digital computers and Number systems.pptxBhawaniShankarSahu1
 
Introduction to C Language
Introduction to C LanguageIntroduction to C Language
Introduction to C LanguageTarun Sharma
 
Object Oriented Programming (OOP) using C++(Freebooks.pk).pdf
Object Oriented Programming (OOP) using C++(Freebooks.pk).pdfObject Oriented Programming (OOP) using C++(Freebooks.pk).pdf
Object Oriented Programming (OOP) using C++(Freebooks.pk).pdfabdullahwale
 
Combinational circuit and Sequential circuit
Combinational circuit and Sequential circuitCombinational circuit and Sequential circuit
Combinational circuit and Sequential circuitPoornima Santhosh
 
Mi 291 chapter 6 (aethetics in engineering design)(1)
Mi 291 chapter 6 (aethetics in engineering design)(1)Mi 291 chapter 6 (aethetics in engineering design)(1)
Mi 291 chapter 6 (aethetics in engineering design)(1)varun teja G.V.V
 
Turing Machine
Turing MachineTuring Machine
Turing MachineRajendran
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures topu93
 
"BCD TO 7 SEGMENT DISPLAY DECODER"
"BCD TO 7 SEGMENT DISPLAY DECODER""BCD TO 7 SEGMENT DISPLAY DECODER"
"BCD TO 7 SEGMENT DISPLAY DECODER"FAIZAN SHAFI
 

Tendances (20)

AI Lab Manual.docx
AI Lab Manual.docxAI Lab Manual.docx
AI Lab Manual.docx
 
Computational Thinking
Computational ThinkingComputational Thinking
Computational Thinking
 
Coal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly ProgrammingCoal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly Programming
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Data types in C
Data types in CData types in C
Data types in C
 
Digital Electronics Notes
Digital Electronics Notes Digital Electronics Notes
Digital Electronics Notes
 
Computational Thinking: Why It is Important for All Students
Computational Thinking: Why It is Important for All StudentsComputational Thinking: Why It is Important for All Students
Computational Thinking: Why It is Important for All Students
 
STEM vs. STEAM
STEM vs. STEAMSTEM vs. STEAM
STEM vs. STEAM
 
Introduction to digital computers and Number systems.pptx
Introduction to digital computers and Number systems.pptxIntroduction to digital computers and Number systems.pptx
Introduction to digital computers and Number systems.pptx
 
IMPLEMENTATION OF GATES USING DIODES
IMPLEMENTATION OF GATES USING DIODESIMPLEMENTATION OF GATES USING DIODES
IMPLEMENTATION OF GATES USING DIODES
 
Introduction to C Language
Introduction to C LanguageIntroduction to C Language
Introduction to C Language
 
Object Oriented Programming (OOP) using C++(Freebooks.pk).pdf
Object Oriented Programming (OOP) using C++(Freebooks.pk).pdfObject Oriented Programming (OOP) using C++(Freebooks.pk).pdf
Object Oriented Programming (OOP) using C++(Freebooks.pk).pdf
 
Combinational circuit and Sequential circuit
Combinational circuit and Sequential circuitCombinational circuit and Sequential circuit
Combinational circuit and Sequential circuit
 
Mi 291 chapter 6 (aethetics in engineering design)(1)
Mi 291 chapter 6 (aethetics in engineering design)(1)Mi 291 chapter 6 (aethetics in engineering design)(1)
Mi 291 chapter 6 (aethetics in engineering design)(1)
 
Verilog lab mauual
Verilog lab mauualVerilog lab mauual
Verilog lab mauual
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Digital Logic circuit
Digital Logic circuitDigital Logic circuit
Digital Logic circuit
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
"BCD TO 7 SEGMENT DISPLAY DECODER"
"BCD TO 7 SEGMENT DISPLAY DECODER""BCD TO 7 SEGMENT DISPLAY DECODER"
"BCD TO 7 SEGMENT DISPLAY DECODER"
 

Similaire à Lesson 3.2 data types for memory location

Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in Cbabuk110
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)Dilawar Khan
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in CUri Dekel
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageGOKULKANNANMMECLECTC
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxRonaldo Aditya
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Blue Elephant Consulting
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Blue Elephant Consulting
 
Programming Fundamentals lecture 6
Programming Fundamentals lecture 6Programming Fundamentals lecture 6
Programming Fundamentals lecture 6REHAN IJAZ
 

Similaire à Lesson 3.2 data types for memory location (20)

Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
 
C#
C#C#
C#
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
 
Aspdot
AspdotAspdot
Aspdot
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c language
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
Theory1&2
Theory1&2Theory1&2
Theory1&2
 
Programming Fundamentals lecture 6
Programming Fundamentals lecture 6Programming Fundamentals lecture 6
Programming Fundamentals lecture 6
 

Plus de MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Dernier

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 

Dernier (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

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