Data Types in C++
Data Types in C++
In this tutorial you will learn about different types of data types in C++. You will read about enumerated data types, primitive data types and modifiers in C++.
While writing computer program in a computer language, we usually need to use variables to store the data taken from user or to store the produced information. This data or information may be of different kinds. Before storing this data/information in variables we need to declare variables and at the time of a variable declaration we will be required to mention that what kind of data/information will be stored in a variable.
For this purpose different data types are available in C++ which can be used to declare variables and tell the computer that what kind of data will be stored in those variables. Once data type is assigned to a variable then the same type of data must be stored in that variable otherwise we will get errors either at the time of compilation or at run time.
Primitive Data Types in C++
The primitive data types available in C++ are as below. All these are predefined and can be used directly without any additional requirements.
Data Type |
Description |
Size in Bytes |
Value Range |
char | Used to store a single character. The character may be alphabetic, numeric or any special character |
1 |
-127 to 127 or 0 to 255 |
int | Used to store integer values |
4 |
-2147483648 to 2147483647 |
float | Used to store floating point values |
4 |
+/- 3.4e +/- 38 (~7 digits) |
double | Double precision floating point values. This data type is considered more accurate and fast than double data type |
8 |
+/- 1.7e +/- 308 (~15 digits) |
bool | Boolean values |
1 |
true or false |
wchar_t | Wide character. It is used for storing compiler-defined wide characters, which may be unicode characters |
2 or 4 bytes |
1 wide character |
void | Used with functions or pointer variables |
NA |
NA |
Modifiers in C++
The value range of some of the above primitive data types can be changed with the signed, unsigned, long and short modifiers as mentioned in the table given below.
Data Type |
Description |
Size in Bytes |
Value Range |
signed char | Signed character |
1 |
-127 to 127 or 0 to 255 |
unsigned char | Unsigned character |
1 |
0 to 255 |
signed int | Values may be either positive, negative or zero |
4 |
-2147483648 to 2147483647 |
unsigned int | Values may be either positive or zero. Negative values are not allowed |
4 |
0 to 4294967295 |
short int | Short integer values |
2 |
-32768 to 32767 |
signed short int | Same as short int |
2 |
-32768 to 32767 |
unsigned short int | Values may be either positive or zero. Negative values are not allowed |
2 |
0 to 65,535 |
long int | Long integer |
4 |
-2,147,483,648 to 2,147,483,647 |
signed long int | Same as long int |
4 |
-2,147,483,648 to 2,147,483,647 |
unsigned long int | Values may be either positive or zero. Negative values are not allowed |
4 |
0 to 4,294,967,295 |
long double | Long double precision |
8 |
+/- 1.7e +/- 308 (~15 digits) |
Enumerated Data Types
Enumerated data types allow us to define our own data types with some specific values. The syntax to define enumerated data type is as below.
enum enum-name { list of values } variables-list;
Here, enum is a keyword followed by the user-defined name. The list of variables are given within curly brackets separated by commas followed by the variables of this data type. An example of enumerated data type is given below.
enum names {Michael,Jhon,Williams,Strac,Jhonson} my_names;
The value of first name is set as 0, the value of second name is set as 1 and son. For example the following code will display 3 as the value of “Starc”.
We can change the default values of names as below. In this case the value of “Michael” is 0 while the value of “Jhon” is set as 10. The value of next name i.e. “Williams” will be set as 11 and so on. So if we display the value of “Starc” then we will get 13 as output.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++
C++ Tutorial
C++ Quizzes