SlideShare a Scribd company logo
1 of 23
Download to read offline
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

What's hot

CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...Asst.prof M.Gokilavani
 
Programming languages and paradigms
Programming languages and paradigmsProgramming languages and paradigms
Programming languages and paradigmsJohn Paul Hallasgo
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesSreedhar Chowdam
 
Data types in C language
Data types in C languageData types in C language
Data types in C languagekashyap399
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and outputOnline
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming LanguageR.h. Himel
 
Python programming
Python programmingPython programming
Python programmingMegha V
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Akshay Nagpurkar
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentationfazli khaliq
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilersVairavel C
 
computer organization and assembly language Lec 01 coal_introduction
computer organization and assembly language  Lec 01 coal_introductioncomputer organization and assembly language  Lec 01 coal_introduction
computer organization and assembly language Lec 01 coal_introductionZia3130
 

What's hot (20)

CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...CS304PC:Computer Organization and Architecture Session 11 general register or...
CS304PC:Computer Organization and Architecture Session 11 general register or...
 
C++
C++C++
C++
 
Programming languages and paradigms
Programming languages and paradigmsProgramming languages and paradigms
Programming languages and paradigms
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Pointers
PointersPointers
Pointers
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming Language
 
Python programming
Python programmingPython programming
Python programming
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Memory Addressing
Memory AddressingMemory Addressing
Memory Addressing
 
Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3
 
Python Interpreter.pdf
Python Interpreter.pdfPython Interpreter.pdf
Python Interpreter.pdf
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentation
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilers
 
computer organization and assembly language Lec 01 coal_introduction
computer organization and assembly language  Lec 01 coal_introductioncomputer organization and assembly language  Lec 01 coal_introduction
computer organization and assembly language Lec 01 coal_introduction
 

Similar to 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
 

Similar to 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
 

More from 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
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 

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