English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ <cmath>

data types ++In this tutorial, we will learn C

in C ++In programming, basic data types such as int, float, char, etc. are used.

int age = 13;

Here, age is a variable of type int. This means that the variable can only store2or4an integer of bytes.

C ++Basic data types

The following table shows the basic data types, their meanings and sizes (in bytes):

typekeywordsize
boolean typebool1 bytes
character typechar1 bytes
integer typeint2 bytes or 4bytes
floating point typefloat4 bytes
double floating point typedouble8 bytes
unspecified typevoid0 bytes
wide character type

wchar_t

2 bytes

Now, let's discuss these basic data types in more detail.

1. C ++ int (integer type)

  • The int keyword is used to represent integers.

  • It is usually the size of4bytes. This means it can store from-2147483648 to 214748647ofvalue.

  • For example,

int salary = 85000;

2. C ++float and double (floating point and double floating point type)

  • float and double are used to store floating-point numbers (decimals and exponents).

  • the size of float is4bytes, the size of double is8bytes. Therefore, the precision of double is twice that of float. For more information, please refer to c++ float and double.

  • For example,

float area = 64.74;
double volume = 134.64534;

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

double distance = 45E12    // 45E12 equals 45*10^12

3. C ++ char (character type)

  • The keyword char is used for characters.

  • Its size is1bytes.

  • C ++The characters in the string are enclosed in single quotes ' '.

  • For example,

char test = 'h';

Note:in C ++in which, integer values are stored in char variables, not the characters themselves.

4. C ++ wchar_t (wide character type)

  • wchar_t is similar to the char data type, the difference being that it is2bytes instead of1bytes.

  • It is used to represent characters that require more memory than a single char to represent them.

  • For example,

wchar_t test = L'ם'  // to store Hebrew characters

Note the letter L before the quotation marks.

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

5. C ++ bool (boolean type)

  • bool data type has two possible values: true or false.

  • bool is used for conditional statements and loops (we will learn about them in later chapters).

  • For example,

bool cond = false;

6. C ++ void (no type)

  • The void keyword indicates the absence of data. This means 'no' or 'no value'.

  • When we study functions and pointers, we will use void.

Note:We cannot declare variables of void type.

C ++type modifiers

We can use type modifiers to further modify some basic data types. C ++there are4type modifiers. They are:

  1. signed

  2. unsigned

  3. short

  4. long

We can use the above modifiers to modify the following data types:

  • int

  • double

  • char

C ++List of data types that can be modified

typebitrange
char1 bytes-128 to 127 or 0 to 255
unsigned char1 bytes0 to 255
signed char1 bytes-128 to 127
int4 bytes-2147483648 to 2147483647
unsigned int4 bytes0 to 4294967295
signed int4 bytes-2147483648 to 2147483647
short int2 bytes-32768 to 32767
unsigned short int2 bytes0 to 65,535
signed short int2 bytes-32768 to 32767
long int4 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long int8 bytes0 to 18,446,744,073,709,551,615
float4 bytesprecision type occupies4bytes (32memory space,+/- 3.4e +/- 38 (~7 a digit)
double8 bytesdouble precision type occupies8 bytes (64memory space,+/- 1.7e +/- 308 (~15 a digit)
long double16 byteslong double type 16 bytes (128bytes of memory space, which can provide18-19digits of precision.
wchar_t2 or 4 bytes1 width characters

Let's look at some examples.

long b = 4523232;
long int c = 2345342;
long double d = 233434.56343;
short d = 3434233; // Error! Out of range
unsigned int a = -5;    //Error! Only positive numbers or 0 can be stored

Derived Data Types

Derived data types are derived from basic data types. For example: arrays, pointers, function types, structures, etc.

We will learn these derived data types in later tutorials.