0% found this document useful (0 votes)
15 views

4 VariablesAndDataTypes-Slides

The document discusses computer memory and different number systems including binary, octal, decimal, and hexadecimal. It explains how numbers are represented in each system and the range of values that can be represented using different numbers of digits.

Uploaded by

Siddhesh18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

4 VariablesAndDataTypes-Slides

The document discusses computer memory and different number systems including binary, octal, decimal, and hexadecimal. It explains how numbers are represented in each system and the range of values that can be represented using different numbers of digits.

Uploaded by

Siddhesh18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

Slides

Section 4 : Variables
and data types
1
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Variables and Data Types

2
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
int main(){} Entry Point Statements Functions

Errors Warnings Input Output

Dev Memory Execution


Comments
Workflow Model Model
3
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
4
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
int double float char

bool void auto …

5
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Memory is a grid of cells that contain either 1 or 0 in them.
Each cell is a bit

We group them in groups of 8, 16,32, 64 cells depending on the size of


Data we want to store

6
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
7
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
8
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
9
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22 23.8

“Steve” …

10
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

11
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Number Systems

• Binary
• Octal
• Hexadecimal

12
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Number Systems

13
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22 23.8

“Steve” …

14
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
2371 2 × 103 + 3 × 102 + 7 × 101 + 1 × 100

Base 10 924 9 × 102 + 2 × 101 + 4 × 100

47 4 × 101 + 7 × 100
15
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
100101 1 × 25 +0 × 24 +0 × 23 + 1 × 22 + 0 × 21 + 1 × 20

Base 2 10010 1 × 24 +0 × 23 + 0 × 22 + 1 × 21 + 0 × 20

111 1 × 22 + 1 × 21 + 1 × 20
16
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Binary Decimal
000 0
001 1
010 2
3 Digits 011 3
100 4
101 5
110 6
111 7

17
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
3 Digits
in memory

18
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Binary Decimal
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
4 Digits 0111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14 19
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
1111 15
4 Digits
in memory

20
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Binary Decimal Binary Decimal
00000 0 10000 16
00001 1 10001 17
00010 2 10010 18
00011 3 10011 19
00100 4 10100 20
00101 5 10101 21
00110 6 10110 22
5 Digits 00111 7 10111 23
01000 8 11000 24
01001 9 11001 25
01010 10 11010 26
01011 11 11011 27
01100 12 11100 28
01101 13 11101 29
01110 14 11110 30 21
The C++ Masterclass : The Fundamentals
01111 ©15
Daniel Gakwaya 11111 32
5 Digits
in memory

22
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Digits Data Range
1 0 ~1
2 0~3
3 0~7
Generalization
4 0~15
5 0~31
… …
n 0~2𝑛−1

23
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Digits Bytes Data Range
8 1 0 ~255
In practice 16 2 0~65,535
32 4 0~34,359,738,367
64 8 0~18,446,744,073,709,551,615

24
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Hexadecimal
System

25
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Binary Decimal Hex
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
Hexadecimal
System 0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E 26
The C++ Masterclass : The Fundamentals 1111
© Daniel Gakwaya
15 F
Hexadecimal 6 E 3 0 F 1 3 F
System

0x 6E30F13F

27
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
1 0010 0100 1000 1011 1010

Padding

0001 0010 0100 1000 1011 1010

28
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Binary Decimal Oct
000 0 0
001 1 1
010 2 2
011 3 3
100 4 4
Octal System
101 5 5
110 6 6
111 7 7

29
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
1 5 6 1 4 1 7 0 4 7 7
Octal System

0 15614170477

30
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Binary Table https://kb.iu.edu/d/afdl

31
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
32
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
• All data is represented by a bunch of grouped cells of 0’s and
1’s in memory
• As the range of your data grows, so will the number of digits you
need to represent the data in memory
• Hexadecimal system makes it a little easier for humans to handle
streams of data with 1’s and 0’s
• Octal has the same goal as Hexadecimal, but it’s almost no
longer used in modern times. It’s just mentioned here for your
awareness

33
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22 23.8

“Steve” …

34
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

35
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Integers

36
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22 37

55939 472299

37
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
A named piece of memory that you use to store
variable
specific types of data.

38
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
age

39
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
• Stores decimals
int • Typically occupies 4 bytes
or more in memory

40
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Variable braced initialization

41
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Functional variable initialization

42
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Assignment Initialization

43
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
• Braced Initialization
• Functional Initialization
• Assignment Initialization

44
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Size of a type in memory

45
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Int’s
in memory

46
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
47
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

48
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Integer Modifiers

49
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22 37

55939 472299

50
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Positive and negative numbers

51
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
52
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
53
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
54
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
unsigned range [0 ~ 2𝑛 − 1]

signed range [- 2𝑛−1 ~ 2𝑛−1 − 1]

55
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
* n is the number of bits for a type in memory
Type with modifier Bytes in memory Range
unsigned int 4 [0, 4,294,967,295]
signed int 4 [-2,147,483,648, 2,147,483,647]

56
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
short long

57
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
58
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
These modifiers only apply to integral types : those
Note
in which you can store decimal numbers

59
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

60
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Fractional Numbers

61
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22.37 37.076

55.939 4722.99

62
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Floating Point Types

63
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Used to represent numbers with fractional parts in C++

64
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22.37 37.076

55.939 4.72299

65
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Type Size Precision Comment
float 4 7 -
double 8 15 Recommended
default
long double 12 > double

66
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Precision

1.23456700001

67
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
68
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Precision

69
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Precision gone wrong

70
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Problem not caught at compile time

71
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Scientific Notation

72
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
73
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Floating Point Numbers Memory Representation

IEEE_754

https://en.wikipedia.org/wiki/IEEE_754

74
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
n(floating point)/ 0 Infinity(+/-)

0.0/ 0.0 NaN

75
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
76
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
• Remember the suffixes when initializing floating point variables,
otherwise the default will be double
• Double works well in many situations, so you will see it used a lot

77
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

78
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Booleans

79
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
true false

80
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
81
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
82
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
83
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
• A byte can store 256 different values
• Using it just to cover two states (true/false) is wasteful, especially for
devices with hard memory constraints (think embedded devices)
• There are techniques to pack even more data into a byte . We’ll learn
more about these in a few upcoming chapters

84
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

85
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Characters and Text

86
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
87
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
88
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
89
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
1 byte : 256 Possible
Each matched to some character
values

90
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
ASCII Table

91
The C++ Masterclass : The Fundamentals © Daniel Gakwaya

* Source : https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
It is possible to assign a valid ASCII code to a char variable, and the
corresponding character will be stored in. You can choose to interpret
that either as a character or a regular integral value

92
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
93
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
• ASCII was among the first encodings to represent text in a computer.
• It falls short when it comes to representing languages other than English and a few
western languages. Think Arabic, East Asian Languages like Japanese, Chinese ,…
• There are better ways to represent text that is meant to be seen in different
languages, one of the most common being Unicode
• The details of Unicode are out of scope for this course, just know that it’s a robust
way to represent text in different languages for a computer

94
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

95
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Auto

96
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
auto Let the compiler deduce the type

97
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
98
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
99
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

100
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Assignments

101
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
102
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
103
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
104
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
105
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

106
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Variables and data types

107
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
int double float char

bool void auto …

108
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
109
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
22 23.8

“Steve” …

110
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Number Systems

• Binary
• Octal
• Hexadecimal

111
The C++ Masterclass : The Fundamentals © Daniel Gakwaya
Slide intentionally left empty

112
The C++ Masterclass : The Fundamentals © Daniel Gakwaya

You might also like