DATA TYPES IN C++
CONTENTS
❖Data type
❖Built In type
❖User defined type
❖Derived type
DATA TYPE
➢A data type is a classification that
specifies the type of value a
variable can hold.
➢General syntax of declaration of
data type is
data_type variable_name;
➢Example: int a;
C++ Data type
User defined Built-In Derived
type type type
C++ Data Type
Built-In Type
Integral Floating Type
Type
Void
INTEGRAL DATA TYPE
Integer: The keyword used for
integer data types is int. Integers
typically require 4 bytes of
memory space and range from -
2147483648 to 2147483647.
Example:
int=85000; SHORT LONG
INT
INT INT
➢Character: Character data type is used for
storing characters. The keyword used for the
character data type is char. Characters
typically require 1 byte of memory space and
range from -128 to 127 or 0 to 255.
Example: char myChar = 'A’;
➢ Boolean: Boolean data type is used for storing
Boolean or logical values. A Boolean variable
can store either true or false. The keyword
used for the Boolean data type is bool.
Example: bool isTrue = true;
bool isFalse = false;
VOID DATA TYPE
➢ The void data type in C++ is used to indicate that
a function does not return any value or to declare
generic pointers that do not point to a specific
data type.
➢ Example:
void myFunction()
{
// This function does not return a value
}
void* genericPointer;
➢ This code declares a generic void pointer named
"genericPointer" as well as the void function
"myFunction," which has no return value.
FLOATING DATA TYPE
➢ Floating Point: Floating Point data type is used for
storing single-precision floating-point values or
decimal values. The keyword used for the floating-
point data type is float. Float variables typically
require 4 bytes of memory space.
Example: float myFloat = 3.14159;
➢ Double Floating Point: Double Floating Point data type
is used for storing double-precision floating-point
values or decimal values. The keyword used for the
double floating-point data type is double. Double
variables typically require 8 bytes of memory space.
Example: double myDouble = 3.141592653589793;
C++ Data type
User-defined type
Structure Union Class Enumeration
USER DEFINED TYPE
STRUCTURES:
➢Structures are used for grouping
together elements with dissimilar types.
➢The general syntax of structure:
Struct name
{
Data_type member1;
…………
};
UNION:
➢Unions are conceptually similar to
structures as they allow us to group
together dissimilar type elements inside
a single unit.
➢The size of union is equal to the size of its
largest member element.
CLASSES:
➢The entire set of data and code of an
object can be made a user-defined data
type with the help of classes.
➢Once a class has been defined, we can
create any number of objects belonging
to that class.
➢Example: fruit mango;
➢Will create an object mango belonging to
the class fruit.
ENUMERATED DATA TYPE:
➢An enumerated data type is another user
defined data type which provides a way
for attaching numbers to names.
➢General syntax of enumerated data type
is:
Enum identifier
{
Value1,value2,……..,Value n
};
DERIVED TYPE
ARRAYS:
➢An array is a fixed sequence collection of
elements of same data type that share a common
name.
➢General syntax to declare is
Data_type name [size];
➢General syntax to initialize is
For(i=0;i<=10;i++)
Cin>>a[i];
FUNCTIONS:
➢A function is a group of statements that
together perform a task.
➢Every c++ program has atleast one
function, which is
void main( )
{
Statements;
}
POINTERS:
➢A pointer is a variable that holds a
memory address.
➢General syntax to declare pointer
is :
Data_type*pointer_name;
➢Example: int *p;
The lists of modifiers used
in C++ are:
❖signed
❖unsigned
❖long
❖short
DATA TYPES SIZE RANGE
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967,295
-2,147,483,648 to
int 4
2,147,483,647
-2,147,483,648 to
long int 4
2,147,483,647
unsigned long int 4 0 to 4,294,967,295
long long int 8 -(2^63) to (2^63)-1
0 to
unsigned long long int 8
18,446,744,073,709,551,615
signed char 1 -128 to 127
unsigned char 1 0 to 255
float 4 -3.4×10^38 to 3.4×10^38
double 8 -1.7×10^308 to1.7×10^308
long double 12 -1.1×10^4932 to1.1×10^4932
wchar_t 2 or 4 1 wide character