0% found this document useful (0 votes)
46 views10 pages

ICP Lecture 7: Syed Nasir Mehdi Nasirsmehdi@ciitsahiwal - Edu.pk

This document provides an overview of scope, type conversion, overflow and underflow, and type casting in C++. It discusses that variables have scope defined by their definition, and automatic type conversion occurs between operands of different types based on data type ranking. Integer division discards the fractional part. Overflow and underflow occur when a value is too large or small for its data type. Type casting allows manual conversion between data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views10 pages

ICP Lecture 7: Syed Nasir Mehdi Nasirsmehdi@ciitsahiwal - Edu.pk

This document provides an overview of scope, type conversion, overflow and underflow, and type casting in C++. It discusses that variables have scope defined by their definition, and automatic type conversion occurs between operands of different types based on data type ranking. Integer division discards the fractional part. Overflow and underflow occur when a value is too large or small for its data type. Type casting allows manual conversion between data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ICP Lecture 7

Syed Nasir Mehdi


[email protected]
Outline
• Scope
• Type conversion
• Overflow and underflow
• Type casting
Scope
• Every variable has a scope
• A variable’s scope is the part of the program that has
access to the variable.
• 1st rule of scope you should learn is that a variable cannot
be used in any part of the program before the definition.

int main()
{
cout << value; // ERROR! value not defined yet!
int value = 100;
return 0;
}
Type Conversion
• When an operator’s operands are of different
data types, C++ will automatically convert
them to the same data type. This can affect
the results of mathematical expressions.
• Just like officers in the military, data types are
ranked. One data type outranks another if it
can hold a larger number. For example, a float
outranks an int.
Type Conversion
Data type ranking
• long double
• double
• float
• unsigned long
• long
• unsigned int
• int
Type conversion
• Rule 1: chars, shorts, and unsigned shorts are automatically promoted to int.
• Rule 2: When an operator works with two values of different data types, the lower
ranking value is promoted to the type of the higher-ranking value.
• In the following expression, assume that years is an int and interestRate is a float:
years * interestRate
• In the following statement, assume that area is a long int, while length and width
are both ints:
• area = length * width;
• Rule 3: When the final value of an expression is assigned to a variable, it will be
converted to the data type of that variable
• In the following statement, assume that area is a long int, while length and width
are both ints:
• area = length * width;
• Since length and width are both ints, they will not be converted to any other data
type. The
• result of the multiplication, however, will be converted to long so it can be stored
in area.
Type conversion
• int x, y = 4;
• float z = 2.7;
• x = y * z;
Type conversion
Integer Division
• When you divide an integer by another
integer in C++, the result is always an integer
double parts;
parts = 15 / 6;
• It doesn’t matter that parts is declared as a
double because the fractional part of the
result is discarded before the assignment
takes place.
Overflow and underflow
• When a variable is assigned a value that is too large or too small in range for that variable’s data
type, the variable overflows or underflows
• int main()
• int main()
{
// testVar is initialized with the maximum value for a short.
short testVar = 32767;

// Display testVar.
cout << testVar << endl;

// Add 1 to testVar to make it overflow.


testVar = testVar + 1;
cout << testVar << endl;

testVar = testVar - 1;
cout << testVar << endl;
return 0;
}
Type Casting
• Type casting allows you to perform manual
data type conversion.

You might also like