English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
The following table shows the basic data types, their meanings and sizes (in bytes):
type | keyword | size |
---|---|---|
boolean type | bool | 1 bytes |
character type | char | 1 bytes |
integer type | int | 2 bytes or 4bytes |
floating point type | float | 4 bytes |
double floating point type | double | 8 bytes |
unspecified type | void | 0 bytes |
wide character type | wchar_t | 2 bytes |
Now, let's discuss these basic data types in more detail.
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;
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
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.
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.
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;
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.
We can use type modifiers to further modify some basic data types. C ++there are4type modifiers. They are:
signed
unsigned
short
long
We can use the above modifiers to modify the following data types:
int
double
char
type | bit | range |
---|---|---|
char | 1 bytes | -128 to 127 or 0 to 255 |
unsigned char | 1 bytes | 0 to 255 |
signed char | 1 bytes | -128 to 127 |
int | 4 bytes | -2147483648 to 2147483647 |
unsigned int | 4 bytes | 0 to 4294967295 |
signed int | 4 bytes | -2147483648 to 2147483647 |
short int | 2 bytes | -32768 to 32767 |
unsigned short int | 2 bytes | 0 to 65,535 |
signed short int | 2 bytes | -32768 to 32767 |
long int | 4 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
signed long int | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int | 8 bytes | 0 to 18,446,744,073,709,551,615 |
float | 4 bytes | precision type occupies4bytes (32memory space,+/- 3.4e +/- 38 (~7 a digit) |
double | 8 bytes | double precision type occupies8 bytes (64memory space,+/- 1.7e +/- 308 (~15 a digit) |
long double | 16 bytes | long double type 16 bytes (128bytes of memory space, which can provide18-19digits of precision. |
wchar_t | 2 or 4 bytes | 1 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 are derived from basic data types. For example: arrays, pointers, function types, structures, etc.
We will learn these derived data types in later tutorials.