In this tutorial you will learn about the C++ Data Types and its application with practical example.
C++ Data Types
Variables are used to represent reserved memory locations that is used to store values, when we create a variable we are a suppose to allocate some memory space for that variable. C++ is a statically typed programming language. This means that variables always have a specific type and that type cannot change. Every variable have data type associated to it, data type for a variable defines –
- The amount of memory space allocated for variables.
- A data type specifies the possible values for variables.
- The operations that can be performed on variables.
C++ has following built-in data types –
- Numbers
- Boolean
- Characters
- Void
C++ Numbers:- The Number data type is used to hold the numeric values. C++ supports following numerical data types –
C++ int:- Integers are used to store whole numbers. C++ supports several integer types, varying internal sizes for storing signed and unsigned integers. Integers can be declared using int keyword.
Syntax:-
1 |
int <variable name>; |
Example:-
1 2 3 |
int num1; short int num2; long int num3; |
Type | Storage size | Value range |
---|---|---|
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 bytes | 0 to 4,294,967,295 |
C++ float:- A Float type is used to store numbers that contain a decimal component (real numbers). In C++, float is used to represent a 32-bit floating-point number and numbers with smaller decimal points. An uninitialized float has default value of 0.0. Float can be declared using float keyword.
Syntax:-
1 |
float <variable name>; |
Example:-
1 |
float num1; |
Type | Storage size | Value range | Precision |
---|---|---|---|
float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places |
C++ double:- In C++, double is used to represent a 64-bit floating-point number and numbers with larger decimal points. An uninitialized double has default value of 0.0. Double can be declared using double keyword.
Syntax:-
1 |
double <variable name>; |
Example:-
1 2 |
double num2; long double num3; |
Type | Storage size | Value range | Precision |
---|---|---|---|
double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places |
C++ Boolean:- The Boolean data type is used to represent the truth values, which can be either True or False. Boolean are commonly used in decision making statements. Boolean can be declared using bool keyword.
Syntax:-
1 |
bool <variable name>; |
Example:-
1 |
bool b1 = true; // declaring a boolean with true value |
C++ Characters:- The character data type is used to hold the single literal. Character are declared char keyword.
Syntax:-
1 |
char <variable name>; |
Example:-
1 |
char ch = 'a'; |
Type | Storage size | Value range |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
C++ void :- The void type means no values. It can not be used with variable declaration.It is usually used with function to specify its return type or its arguments.
Example:
1 2 3 4 |
void echo_square(int Number) { printf("%d squared is %d\n",Number, Number*Number); } |
User defined type declaration using typedef
In C++, you are allowed to create new data type. But it is usually created using existing data type with another identifier.This user defined data type identifier can later be used to declare variables. In short its purpose is to redefine the name of an existing data type.
Syntax:-
1 |
typedef <type> <identifier>; |
Example:-
1 |
typedef int number; |
Now we can use number identifier instead of int to declare integer variable.
Enum Data Type
This is an user defined data type having finite set of enumeration constants or aliases.
Syntax:-
1 |
enum [data_type] {const1, const2, ...., const n}; |
Example:-
1 |
enum boolean {FALSE=0, TRUE }; |