Lecture 2
Lecture 2
21
08/10/2024
Variable
To store a value inside a computer a ‘variable’ is
used.
A variable is a space in the memory to store a value. This
space is reserved until the variable is required.
08/10/2024 22
An Example of a Variable
08/10/2024 23
Memory (RAM)
Program Memory. 1 Byte
.
• Each block in memory represents 204
1 byte 205
206
207
208
209
210
211
.
.
Memory (RAM)
Program Memory. 1 Byte
.
• Each block in memory represents 204
1 byte 205
206
• Each byte in memory has an 207
address 208
209
210
• Whenever a variable is declared,
211
the computer reserves some .
memory for it. .
Memory (RAM)
Program Memory. 1 Byte
.
Integers are stored in 4 bytes 204
Characters stored in 1 byte 205 $
206
int num = 100; 207
char letter = ‘$’; 208
209
210 10
211
.
0
.
Memory (RAM)
Program Memory. 1 Byte
.
Integers are stored in 4 bytes 204
Characters stored in 1 byte 205 $
206
int num = 100; 207
char letter = ‘$’; 208
209
210 10
Can we find the memory address of
these variables??
211
.
0
.
Memory (RAM)
Program Memory. 1 Byte
.
Integers are stored in 4 bytes 204
205 $
int num = 100; 206
char letter = ‘$’; 207
208
Can we find the memory address of 209
210 10
these variables??
211
.
0
cout<< # .
cout<< &letter;
Memory (RAM)
Program Memory. 1 Byte
.
int num = 100; 204
char letter = ‘$’; 205 $
206
cout<< # 207
cout<< &letter; 208
209
210 10
Variable Name Data Type
num int
Starting Address
208
211
.
0
letter char 205 .
Memory (RAM)
Program Memory. 1 Byte
.
int num = 100; 204
char letter = ‘$’; 205 $
206
cout<< # //prints 208 207
cout<< &letter; //prints 205 208
209
210 10
Variable Name Data Type
num int
Starting Address
208
211
.
0
letter char 205 .
Example of a Variable
(Memory View)
int temperature = 35
00000000 Location 0
Locations 0 - 3 are collectively 00000000 Location 1
called as ‘temperature’ 00000000 Location 2
00100011 Location 3
Location 4
08/10/2024 31
Changing the Value of Variable
08/10/2024 32
Variable types
Variable type Keyword used in Size in bits Range
declaration
integer int 32 bits -2147483648 to
2147483647
Short integer short int 16 bits -32768 to 32767
x = 5671
08/10/2024 36
Variable Types
• To use a variable in our code
– First, we must have to declare it, variable are known with its
keywords. “Use the name of keyword” for declaration of a
variable”.
– Example: int number1 = 10;
– Example: float floatData = 20.93;
– Example: long int myData = -10;
– Example: char textData = ‘A’;
– Example: bool boolData = true;
#include <iostream> #include <iostream>
using namespace std; using namespace std;
void main () void main ()
{ {
int number1 = 10; int number1 = 20,
float floatData = number2 = 10;
20.93; }
}
08/10/2024
Points to remember in naming the
variables
• The names given to variables (and other
program features) are called identifiers.
• What are the rules for writing identifiers?
• You can use upper and lowercase letters, and the
digits from 1 to 9.
• You can also use the underscore (_).
• The first character must be a letter or
underscore. (it cannot be a digit)
• No spaces allowed in a variable name
08/10/2024 38
Program 1
Program 2
Escape Sequence
Examples:
cout << "Hello\t" << "I\’m Ali\n";
cout << "123\nabc
Example
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
– Block Comment
• /* This is a Block Comment.*/