3 - Data Types in C++
3 - Data Types in C++
1
C++ Data Types
While doing programming in any programming language, you need to use
various variables to store various information. Variables are nothing but
reserved memory locations to store values. This means that when you
create a variable you reserve some space in memory.
You may like to store information of various data types like character,
integer, floating point, double floating point, Boolean etc. Based on the
data type of a variable, the operating system allocates memory and decides
what can be stored in the reserved memory.
2
• As its name indicates, a data type represents a type of the data which
you can process using your computer program. It can be numeric,
alphanumeric, decimal, etc.
• A data type is used to Identify the type of a variable when the variable is
declared .
Data type:
Void
Char
int
long
Float(real)
Double
3
pointer
Two Classifications of Data Types
4
Fundamental Data Types :
• void – used to denote the type with no values
• int – used to denote an integer type
• char – used to denote a character type
• float, double – used to denote a floating point type
• int *, float *, char * – used to denote a pointer type, which is a memory
address type
Type Typical Bit Width Value range which can be represented by this data type
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
} 6
This example uses endl, which inserts a new-line character after every line and <<
operator is being used to pass multiple values out to the screen. We are also using sizeof()
function to get size of various data types.
When the above code is compiled and executed, it produces the following result which
can vary from machine to machine:
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4 7
Thanks