c++

C++ Data Types

In C++, data types are declarations for variables. This determines the type and size of data associated with variables. Data type are responsible to store different type of values and data in the variable. Data type is atype of data stored in the form of variable.

For example:

int roll no = 21;
Float average = 30.43;

In the example above age is a variable of type int Meaning, the variable can only store integers of either 2 or 4 bytes and in the second example the variable can store 4 byte.


C++ Fundamental Data Types

Data Type Meaning Size (in Bytes)
int Integer 2 or 4
float Floating-point 4
double Double Floating-point 8
char Character 1
wchat_t Wide Character 2
bool Boolean 1
void Empty 0

A. C++ int

  1. The int keyword is used to show the integers values to be stored in the variable .
  2. Its size is usually 2 or 4 bytes. Which means it can store values from -2147483648 to 214748647.

For example:

int salary = 37000;
int num = 54;

B. C++ float and double

  1. Float and double are used to store real numbers ,decimals and exponentials number .
  2. The size of float is 4 bytes and the size of double is 8 bytes. Double has two times the precision of float . To learn more, visit C++ float and double.

For example:

float area = 34.44;
double volume = 120.64534;

As mentioned above, these two data types are also used for exponentials. For example,

double distance = 45E12

C. C++ char

  1. char keyword is used to show character value to be stored in variable.
  2. char consume size is 1 byte.
  3. Characters in C++ are written inside single quotes ‘ ’ .

For example:

char test = 'p';

D. C++ wchat_t

  1. Wide character wchar_t is similar to the char data type,except its size is 2 bytes instead of 1
  2. 2. 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


E. C++ bool

  1. The bool data type has one of two possible values: true or false .
  2. Booleans are used in conditional statements and loops

For example:

bool cond = true;

F. C++ void

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


C++ Type Modifiers

We can further modify some of the fundamental data types by using type modifiers. There are 4 type modifiers in C++. They are:

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

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

  1. int
  2. double
  3. char

C++ Modified Data Types List
Data Type Size (in Bytes) Meaning
signed int 4 used for integers (equivalent to int)
unsigned int 4 can only store positive integers
short 2 used for small integers (range -32768 to 32767)
long at least 4 used for large integers (equivalent to long int)
unsigned long 4 used for large positive integers or 0 (equivalent to unsigned long int)
long long 8 used for very large integers (equivalent to long long int).
unsigned long long 8 used for very large positive integers or 0 (equivalent to unsigned long long int)
long double 8 used for large floating-point numbers
signed char 1 used for characters (guaranteed range -127 to 127)
unsigned char 1 used for characters (range 0 to 255)

Example:

long b = 7635283;
long int c = 9302836;
long double d = 233434.56343;
short d = 3434233; // Error! out of range
unsigned int a = -5; // Error! can only store positive numbers or 0

Derived Data Types

Derived data type are those data type which are derived from fundamental data type.Derived data types are arrays, structures, pointers etc.Integers are used to store integer type data, not the floating point number.