C++ Data Types


Data types is a data storage which declaring variables of different types such as integer, floating, character and etc.

Data types is a data storage format that can contain a specific type or range of values. The kind of data that variables may hold in a programming language is called as data types.

Based on the data type the OS allocates the memory and decides what can store in the reserved memory.

C++ datatypes

In C++ language four types of data types:

  • Primary data types - int, char, float, double, etc
  • User-defined data types - structure
  • Derived data types - array, pointer, etc
  • Enumeration Data Type - enum

1. Primary Data Types:

There are four fundamental data types. They are an integer (int), character (char), floating point (float), double floating point (double).


Integer:

It is used to represent integers in C++. Int data type range should be include -32767 to 32767.

int variable_name;

Character:

It represents an individual character value i.e. a letter, a digit, or a specific symbol. Each type char value is enclosed in single quotes.

char variable_name;

Floating Point:

In this numbers are stored in 32 bits, with 6 digits of precision.

float variable_name;

Double Floating Point:

This type of data type is used to represent real numbers. It uses 64 bits with 14 digits precision.

double variable_name;

2. User-defined data types:

It enables a program to invent his own data types. User-defined data types can help a programmer to reduce programming errors.


3. Derived data type:

It is created from the basic integers, characters, and floating data types. Ex: Arrays, Pointers, etc..


4. Enumerated Data Types

Enumerated Data Types must use the keyword of "enum".

 enum enum-name { list of names } var-list;

Data Types Storage size Range value
char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
long 4 byte -2,147,483,648 to 2,147,483,647
unsigned long 4 byte 0 to 4,294,967,295
float 4 byte 1.2E-38 to 3.4E+38
double 8 byte 2.3E-308 to 1.7E+308
long double 10 byte 3.4E-4932 to 1.1E+4932

Example:

#include <iostream>
using namespace std;
                    
int main() 
{ 
	cout << "Storage size for character: "<< sizeof(char) << endl;
	cout << "Storage size for int : "<< sizeof(int) << endl;
	cout << "Storage size for float : " << sizeof(float) << endl;
	cout << "Storage size for double : "<< sizeof(double) << endl;
	cout << "Storage size for long : "<< sizeof(long int) << endl;
	return 0;
}



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.