0% found this document useful (0 votes)
10 views39 pages

Slide Set - 10

The document discusses data types and variables in C++. It defines different data types like char, int, float, etc and their ranges. It also explains how variables are declared and defined in C++ by specifying their name, data type and optionally an initial value. The document further explains how variables correspond to memory locations.

Uploaded by

Jahanzaib Brohi
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)
10 views39 pages

Slide Set - 10

The document discusses data types and variables in C++. It defines different data types like char, int, float, etc and their ranges. It also explains how variables are declared and defined in C++ by specifying their name, data type and optionally an initial value. The document further explains how variables correspond to memory locations.

Uploaded by

Jahanzaib Brohi
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/ 39

Slide Set – 10

CP & CPSA
17ME & 17PG
Contents
• Data types in C++ (LL 04)
• Variables in C++ (LL 04)
• Variable declaration in C++ (LL 04)
• Variable definition in C++ (LL 04)
• Constants in C++ (LL 04)
• Variable as memory locations (LL 04)
• Inputting with cin statement (LL 02)
• Problem Examples (LL 04)
LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis
Data Types in C++
Data Type Memory Range Start Range End
char 1 Byte -128 127
unsigned char 1 Byte 0 255
short 2 Bytes -32,768 32,767
unsigned short 2 Bytes 0 65,535
int 4 Bytes -2,147,483,648 2,147,483,647
unsigned int 4 Bytes 0 4,294,967,295
long 8 Bytes -9,223,372,036,854,775,807 9,223,372,036,854,775,807
unsigned long 8 Bytes 0 18,446,744,073,709,551,615
float 4 Bytes 3.4 × 10−38 3.4 × 1038
double 8 Bytes 1.7 × 10−308 1.7 × 10308
bool 1 Byte true and false
Variables in C++
• Variables are used to stored the data temporarily.
• A variable is a named piece of memory location.
• A single variable can store single value at a time.
• The value of the variable is changeable.

In order to create a variable we need to specify three things:


Name
Data Type
Value (optional)
Variables in C++

• The name of the variable is called as the identifier.

• The data type of the variable specifies the type of the value which will be
stored in it.

• The value is the actual content which will be stored in it. It is optional
because some time we know the exact value which will be stored, sometimes
we do not know.
Creating Variables in C++
• There are two ways to create a variable:
Variable Declaration in C++

• A variable is declared when we do not know the value to be stored in it.


• To declare the variable, we need to specify two things: name and data type.

data_type variable_name;
Variable Declaration in C++
float radius;

radius
Variable Declaration in C++

char v1 ; long v7 ;
unsigned char v2 ; unsigned long v8 ;
short v3 ; float v9 ;
unsigned short v4 ; double v10 ;
int v5 ; bool v11 ;
unsigned int v6 ;
Variable Definition in C++

• A variable is defined when we know the exact value to be stored in it.


• To define the variable, we need to specify three things: name, data type and
value.

data_type variable_name = value;


Variable Definition in C++
float radius = 56.214F;

radius

56.214
Variable Definition in C++

char v1 = ‘w’ ; long v7 = 25486655L ;


unsigned char v2 = ‘w’ ; unsigned long v8 = 58622455UL ;
short v3 = -24 ; float v9 = 25.4856F ;
unsigned short v4 = 64 ; double v10 = 5685.2154785 ;
int v5 = 5847 ; bool v11 = true ;
unsigned int v6 = 8552U ;
Constants in C++
• A constant is a variable whose value can not be altered during the execution of the
program.
• A constant is given a single value at the time of creation and it will hold that value
till the entire execution of the program.
• In C++, a variable is declared as constant by just prefixing the variable definition
statement with the keyword const.
const data_type variable_name = constant_value;
const float PI = 3.1415F ;
const int MAX = 100 ;
Variables as Memory Locations
• A variable is defined as a named piece of memory
location. int num = 26 ;
MEMORY
• When we create a variable, some bytes of memory 2000H
are reserved according to the data type, and are 2001H
given a name of the variable. num 2002H
2003H
26
• Entire memory is divided in to pieces of 1 byte 2004H

and each of the byte has address defined in 2005H


hexadecimal number. 2006H
Variables as Memory Locations
• Consider following empty memory MEMORY
2000H
map: 2001H

• Execute the following variable 2002H


2003H
definition statements: 2004H
2005H
2006H
short V1 = 69 ; 2007H
2008H

char V2 = ‘w’ ; 2009H


200AH

int V3 = 5478 ; 200BH


200CH
200DH
float V4 = 87.245F ; 200EH
200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
2002H
2003H
2004H
2005H
2006H

short V1 = 69 ; 2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H

V2 w 2002H

2003H
2004H
2005H
2006H
char V2 = ‘w’ ; 2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H

V2 w 2002H

V3 2003H
2004H
5478 2005H
2006H
int V3 = 5478 ; 2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H

V2 w 2002H

V3 2003H
2004H
5478 2005H
2006H
float V4 = 87.245F ; V4 2007H
2008H
87.245 2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
• Consider following memory map: MEMORY
2000H

• Write down the variable definition V1 2001H


2002H
statements for the variables created in 69.258 2003H
the memory. 2004H
2005H
2006H
V2 2007H
658 2008H
2009H
200AH
V3 200BH
200CH
1453 200DH
200EH

V4 b 200FH

2010H
2011H
Variables as Memory Locations
• Consider following memory map: MEMORY
2000H

• Write down the variable definition V1 2001H


2002H
statements for the variables created in 69.258 2003H
memory. 2004H
2005H
2006H
float V1 = 69.258F ; V2 2007H
658 2008H

short V2 = 658 ; 2009H


200AH

int V3 = 1453 ; V3 200BH


200CH
1453 200DH
char V4 = ‘b’ ; 200EH

V4 b 200FH

2010H
2011H
Variables as Memory Locations
MEMORY
• Execute the following variables V1
2000H
2001H
definition statements in the same 2002H
memory map: 69.258 2003H
2004H
2005H
2006H

short V5 = 985 ; V2
658
2007H
2008H

char V6 = ‘r’ ; 2009H


200AH
V3 200BH
short V7 = 74 ; 200CH
1453 200DH
200EH

V4 b 200FH

2010H
2011H
Variables as Memory Locations
MEMORY
2000H
V1 2001H
2002H
69.258 2003H
2004H
V5 2005H
985 2006H
V2 2007H
short V5 = 985 ; 658 2008H
2009H
200AH
V3 200BH
200CH
1453 200DH
200EH

V4 b 200FH

2010H
2011H
Variables as Memory Locations
MEMORY
V6 r 2000H

V1 2001H
2002H
69.258 2003H
2004H
V5 2005H
985 2006H
V2 2007H
char V6 = ‘r’ ; 658 2008H
2009H
200AH
V3 200BH
200CH
1453 200DH
200EH

V4 b 200FH

2010H
2011H
Variables as Memory Locations
MEMORY
V6 r 2000H

V1 2001H
2002H
69.258 2003H
2004H
V5 2005H
985 2006H
V2 2007H
short V7 = 74 ; 658 2008H
V7 2009H
74 200AH
V3 200BH
200CH
1453 200DH
200EH

V4 b 200FH

2010H
2011H
Inputting with cin

cin >> radius;


Inputting with cin
• In C++, cin statement is used to get input from the keyboard.

• Cin is the Standard Console Input, which is the keyboard.

• Cin is used in conjunction with the extraction operator ( >> ).

• Anything inputted from keyboard will go to the variable to the right side of
>> extraction operator.
Program Examples
Variables and constants in C++
Program Example 01

Problem Statement:
Write a computer program in C++ that accepts the base and height of a right
angle triangle from the user and displays the area of the triangle.
Program Example 01
Program Example 01
Program Example 02

Problem Statement:
A person is running in a circular ground. Write a program in C++ that asks the
user to input the radius of the ground in meters and the number of rounds the
person completes. The program should display the amount of distance
travelled by the person in meters.
Program Example 02
Program Example 02
Program Example 02
Program Example 03

Problem Statement:
Write a program in C++ that asks the user to enter two integer numbers, stores them
in variables num1 and num2 respectively. The program swaps the values of two
variables with each other without using a third variable and displays the values of
both the variables after swapping.

Sample output:
Input: Output:
num1 = 45 num1 = 94
num2 = 94 num2 = 45
Program Example 03
Program Example 03
Program Example 03

You might also like