Fundamental Data Types in C++
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
Reading time: 30 minutes | Coding time: 5 minutes
In C++, every variable use data types to define the type of data it can store.
As, we already know variables are something which stores data.So,data types are used to specify before the compilation that what type of data the variable is going to store.
C++ is said to be stongly typed language which actually means we have to define the data type of the variable before using it.Each data type has its own size in memory.
There are mainly two types of data types in C++:-
- Fundamental data types
- User defined data types
Note:-This article will give you a deep understanding about the fundamental data types.
Fundamental Data types
Fundamental data types are the data types which are predefined in the language and can be directly used to declare a variable in C++.
The various fundamental data types are:
- char (Character)
- int (Integer)
- float (Floating point)
- bool (Boolean)
- double (Double floating point)
- void (valueless)
Now, let's talk about each one of them.
1. Character
- It is the datatype that is used to store characters like a,b,c etc.
- To define Character in C++ we use char keyword.
- Size of char datatype is 1 byte i.e 8 bits.
- It's range is from -128 to 127 or it can be 0-255.
Syntax:-
char a;
where 'a' is variable.
2. Integer
- It is the datatype that is used to store integer values like 1,2,-1 etc.
- To define Integer in C++ we use int keyword.
- Size of int datatype is 4 byte i.e 32 bits.
- It's range is from -2147483648 to 2147483647.
Syntax:-
int a;
3. Floating point
- It is the datatype that is used to store floating values or decimal values like 1.1,2.1 etc.
- To define Floating point in C++ we use float keyword.
- Size of float datatype is 4 byte i.e 32 bits.
- It's range is from +/- 3.4e +/- 38 (~7 digits).
Syntax:-
float a;
4. Boolean
- It is the datatype that is used to store two values only i.e true or false.
- To define Boolean in C++ we use bool keyword.
- Size of bool datatype is 1 byte i.e 8 bits.
Syntax:-
bool a;
5. Double floating point
- It is the datatype that is used to store floating values with higher precision.
- To define Double Floating Point in C++ we use float keyword.
- Size of double datatype is 4 byte i.e 32 bits.
- It's range is from +/- 1.7e +/- 308 (~15 digits).
Syntax:-
double a;
6. Valueless
- It is the datatype that is used to represent a variable without any value.
- To define Valueless in C++ we use void keyword.
- Void can be used as an return type of functions which indicates that function is returning nothing.
Syntax:-
void a;
A simple use of void as a return type of a function in C++:
void printmsg(void)
{
printf("Hello world\n");
}
- Here the return type of function printmsg is void which means it is returning nothing.
- void inside the parenthesis shows that the function has no or valueless arguement.
Data type modifiers
- As the name suggests ,these are used to modify primitive data type i.e int,char,float.
- They are used as a prefix to the primitive data types.
- These modifiers change the size and the type of values that a primitive data type can hold.
There are four modifiers:
- signed:- It is used to represent numbers both in the positive and negative ranges.
Syntax:
signed int a;
"a" can hold both negative and positive values.
- unsigned:- It is used to represent numbers in the positive only.
Syntax:
unsigned int b;
"b" can hold only positive values no negative values.
- short:- It is used to decrease the size of primitive data types.
Syntax:
short int c;
"c" will now take 2 bytes of memory instead of 4 bytes.
- long:- It is used to increase the size of primitive data types.
Syntax:
long int d;
The table below shows the size and range of data types :
Data Type | 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 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 | 0 to 4,294,967,295 |
long long int | 8 | -(2^63) to (2^63)-1 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
float | 4 | +/- 3.4e +/- 38 (~7 digits) |
double | 8 | +/- 1.7e +/- 308 (~15 digits) |
Note: Notice the range in case of unsigned and signed.Also these range can vary according to the compiler.
User defined data types in C++ are categorized into following:
- Classes
- Union
- Structure
- Enumeration
As this article mainly focuses on fundamental data types, we will discuss about user defined data types in separate articles.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.