In computer programming, data types are used to define the nature and characteristics of data stored in variables. They determine the operations that can be performed on the data and the way the data is stored in memory.
These are some of the fundamental data types in programming languages. Depending on the programming language, there may be additional built-in data types or the ability to define custom data types.
2. Variables
During programming, we need to store data.
The data is stored in variables.
Variables are the containers that contain some data
The memory is divided into blocks and each block has its
unique numerical address .
We give names to these memory locations which are
called variables.
We call them variables because they the data stored in
the variable can be changed at different times.
4. Variable Type
A variable must have a Data Type associated with it.
Data type defines the type of data to be store in the
variable.
For example:
• Integer (whole numbers)
• Float (fractional numbers, decimal points)
• Character (alphabet)
• Boolean (true/false)
• String (text)
5. Variable Size
Different Data Types require different memory
space. For example:
Integer (int)
Float (float)
Double (double)
Character (char)
Boolean (bool)
String
4 bytes
4 bytes
8 bytes
1 byte
1 byte
8 bytes
6. Variable Name
We use naming conventions to declare variable names.
Variable should be given name which matches the
nature of the data.
Variable names should be legal. For example
◦ Names can contain a series of characters consisting of
letters, digits and underscores (_)
◦ Cannot start with numbers or special characters
◦ Spaces are not allowed
◦ Keywords or reserved words are not allowed
◦ Uppercase and lowercase letters are different, so a1
and A1 are different variables
7. Scope of Variables
In terms of scope, variable are categorized into two
broad types:
Local Variables
Local variables are those which are declared in the body
of a function. These variables are accessible within the
body of the function.
Global Variables
These variables are declared outside the body of all
functions. These variables are accessible anywhere in
the program.
9. 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;
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.
11. 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.
12. Type Conversion
Rules:
When using assignment operator (=), the type of
expression on right side will be converted into
type of left side variable.
If floating point value is assigned to an integer, it
will be truncated.
If an integer value is assigned to a floating point
value, it will be promoted.
14. 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;