Home C++ Tutorial C++ Data Types

C++ Data Types

by Anup Maurya
30 minutes read

In this tutorial, we will learn about basic data types such as int, float, char, etc. in C++ programming with the help of examples.

Suppose you are in kitchen and you have some container, but the point is you have to store different types of materials in different container some are liquid , some are solid and some requires air tight container and some not. To overcome this problem, you have different types of material to store respective materials as the same way data are of different types some are integers, some are fractional and some character, etc. That’s why we need data types.

What is Data Types?

Data types are declarations for variables. It helps to determines the type and size of data associated with variables.

For example,

int marks = 83; 

Here, marks is a variable of type int. Meaning, the variable can only store integers of either 2 or 4 bytes depending of architecture of machine.

C++ Fundamental Data Types

C++ fundamental data types, their meaning, and their sizes (in bytes) are as follows:

Data TypeMeaningSize (in Bytes)
intInteger2 or 4
doubleDouble Floating-point8
wchar_tWide Character2

Now, let us discuss these fundamental data types in depth,

1. C++ int

  • The int keyword is used to denote integers.
  • Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
  • For example,
int marks = 85;

2. C++ float and double

  • float and double are used to store floating-point numbers (decimals and exponentials).
  • The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the precision of float. To learn more, visit C++ float and double.
  • For example,
float area = 64.74;
double volume = 14.34;

//above two datatypes are also used for exponentials

double distance = 45E12    // 45E12 is equal to 45*10^12

3. C++ char

  • Keyword char is used for characters.
  • Its size is 1 byte.
  • Characters in C++ are enclosed inside single quotes ' '.
  • For example,
char gender = 'M';

4. C++ wchar_t

  • Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1.
  • It is used to represent characters that require more memory to represent them than a single char.
  • For example,
wchar_t test = L'ם'  // storing Hebrew character;

Notice the letter L before the quotation marks.

Note: There are also two other fixed-size character types char16_t and char32_t introduced in C++11.

5. C++ bool

  • The bool data type has one of two possible values: true or false.
  • Booleans are used in conditional statements and loops.
  • For example,
bool mcq_question = false;

6. C++ void

  • The void keyword indicates an absence of data. It means “nothing” or “no value”.
  • We will use void when we learn about functions and pointers.

Note: We cannot declare variables of the void type.

C++ Type Modifiers

If you want to modify some of fundamental data types by using type modifiers, Then C++ have 4 types modifiers. These are as follows

  1. signed
  2. unsigned
  3. short
  4. long

We can modify the following data types with the above modifiers,

  • int
  • double
  • char

C++ Modified Data Types With Size

Data TypeSize (in Bytes)Meaning
signed int4used for integers (equivalent to int)
unsigned int4can only store positive integers
short2used for small integers (range -32768 to 32767)
unsigned short2used for small positive integers (range 0 to 65,535)
longat least 4used for large integers (equivalent to long int)
unsigned long4used for large positive integers or 0 (equivalent to unsigned long int)
long long8used for very large integers (equivalent to long long int).
unsigned long long8used for very large positive integers or 0 (equivalent to unsigned long long int)
long double12used for large floating-point numbers
signed char1used for characters (guaranteed range -127 to 127)
unsigned char1used for characters (range 0 to 255)

Let’s understand with some examples,

long b = 467832;
long int c = 1145342;
long double d = 234.56343;
short d = 3423367; // Error! out of range
unsigned int a = -34;    // Error! can only store positive numbers or 0

Derived Data Types

Data types that are derived from fundamental data types are derived types.

For example: arrays, pointers, function types, structures, etc.

related posts

Leave a Comment