PF 2 - Data Types and Veriables
PF 2 - Data Types and Veriables
Fundamentals
Syntax:
int myAge;
myAge = 35;
Variables Vs Constants
Variables
Those quantities whose values can be changed
throughout the program.
Constants
Those quantities whose values CANNOT be changed
in the program.
Syntax:
const int x = 10;
Type Conversion
Type conversion allows a compiler to convert one
data type to another data type at the time of
compilation of a program.
This is called implicit type conversion.
However, the conversion from one data type to
another is prone to data loss.
Type Conversion
Data loss happens when data of a
larger type is converted to data of
a smaller type.
Rules:
When operating with values of
different data types, the lower
ranked value will be promoted to
higher data type.
Type Conversion
Rules:
When using assignment operator (=), the type of
expression on right side will be converted into
type of left side variable.
int x = 8 + 4.9;
x = 12
float x = 8;
x = 8.0
char x = 65;
x= A
Type Casting
When the user manually changes data from one
type to another, this is known as type casting.
This type of conversion is also called explicit type
conversion.
Syntax;
Static_cast<DataType>(Expression);
OR
(datatype)Expression;