Lec 2 - Memory and Variables
Lec 2 - Memory and Variables
3
Memory
• Imagine a memory as a block with different slots
• Each slot has a size of 1 byte
• 1 byte= 8 bits
• Each memory location has an address (0001,0002,0003, etc..)
4
Variables
• Variables are a portion of memory to store a value
• Each variable has a specified size (1 byte, 4 bytes, etc…)
• Types of Variables:
5
Character Type:
• C++ syntax char
• Size= 1 Byte of the memory
• It can save values such as letters and symbols,
example: ‘A’, ‘$’, etc…
7
Binary and ASCII
• Numbers gets converted from decimal to binary (we covered
that last lecture)
• What about characters? How are they saved in the memory?
• There is an ASCII table which gives every symbol and letter a
binary number.
TYPE NAME
10
Variables
• Example of variables:
char cool;
TYPE NAME
12
Inserting Values and Printing
Messages
• To allow the user to input values that will be saved in the
memory:
1- Define a variable (type and name)/ Ex: int number;
2- Write the syntax cin>> variable name;/ Ex:
13
C++ Program Main Block
#include <iostream>
using namespace std;
int main ()
{
• is a header file library that lets us work with input and output
objects, Header files add functionality to C++ programs
• means that we can use names for objects and variables from
the standard library
24
C++ Variables
34
C++ User Input
int main() {
cout << "Hello World!";
40
Find the sum of 2 numbers
#include <iostream>
using namespace std;
int main ()
{
41
return 0;
}
Find the average of 3 numbers
#include <iostream>
using namespace std;
int main ()
{
42
return 0;
}