In this example, we are going to cover the basic data types in C++. The way of declaring variables is slightly different from Python so you need to be careful here too. I keep comparing C++ to Python because I mainly just Python while I work (Mostly signal processing).
In Python, you don't really think about data types and their sizes because the entire process is automatic and simple. C++, on the other hand, is different. You have to know their data types and sizes correctly to control memory flow. I am not going to explain everything here because it's too much to understand for beginners.
1. Basic data types and their sizes
The below table 1 shows the data types and their memory sizes(1). There are numerous data types but in this stage, you only need to know 5 data types, int - bool - char - float - double. "signed" means the sign of numbers like + and -. All you need to know about the data types is their sizes.
For example, the "int" data type allocates 4 bytes of data into the memory and it means you have 32 bits ( 8 bits = 1 byte ) that can store an integer number up to 31 bits size (2147483648). The reason that the maximum stored number of "int" is not 2^32 is that the first byte is assigned for the sign of a given integer.
Table 1 Data types in C++ | |||
D-Type | Bytes | Other names | Range of values |
int | 4 | signed | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 | unsigned | 0 to 4,294,967,295 |
__int8 | 1 | char | -128 to 127 |
unsigned __int8 | 1 | unsigned char | 0 to 255 |
__int16 | 2 | short, short int, signed short int | -32,768 to 32,767 |
unsigned __int16 | 2 | unsigned short, unsigned short int | 0 to 65,535 |
__int32 | 4 | signed, signed int, int | -2,147,483,648 to 2,147,483,647 |
unsigned __int32 | 4 | unsigned, unsigned int | 0 to 4,294,967,295 |
__int64 | 8 | long long, signed long long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned __int64 | 8 | unsigned long long | 0 to 18,446,744,073,709,551,615 |
bool | 1 | none | false or true |
char | 1 | none | -128 to 127 by default 0 to 255 when compiled by using /J |
signed char | 1 | none | -128 to 127 |
unsigned char | 1 | none | 0 to 255 |
short | 2 | short int, signed short int | -32,768 to 32,767 |
unsigned short | 2 | unsigned short int | 0 to 65,535 |
long | 4 | long int, signed long int | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 | unsigned long int | 0 to 4,294,967,295 |
long long | 8 | none (but equivalent to __int64) | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long long | 8 | none (but equivalent to unsigned __int64) | 0 to 18,446,744,073,709,551,615 |
enum | varies | none | |
float | 4 | none | 3.4E +/- 38 (7 digits) |
double | 8 | none | 1.7E +/- 308 (15 digits) |
long double | same as double | none | Same as double |
wchar_t | 2 | __wchar_t | 0 to 65,535 |
"int" is a data type contains integers (positive-negative numbers and zero) and needs 4 bytes.
"float" is a data type contains single-precision floating-point numbers with up to 7 significant digits (32bits) and needs 4 bytes.
"double" is a data type contains double-precision floating-point numbers with up to 15 significant digits (64bits) and needs 8 bytes.
"bool" is a data type contains one of two possible values (true or false). true is 1 and false is 0 and needs 1 byte.
"char" is a data type contains a single character data in a fixed-length filed. It can only express 255 pre-set characters defined by the ASCII table (ASCII stands for the "American Standard Code for Information Interchange") and needs 1 byte.
1.1 Declaration of variables
How to declare variables is very simple. You define a data type and variable name. The initialization of the variable is optional. Either way, as soon as you declare a variable, C++ allocates 4 bytes space on the memory anyway.
2. Short examples of data types
The below codes contain lots of unknown statements that I assume that you do not know yet, but it is okay. I will explain them later. I just want you to know their sizes and definitions first. "cout" (Console Out) is just a function shows you what you've ordered to output on the screen.
2.1 Examples
Example 1 ) Declare basic data types and get their sizes.
// Example 01
#include <iostream>
int main()
{
int a;
float b;
double c;
bool d;
char e;
std::cout << sizeof(a) << std::endl;
std::cout << sizeof(b) << std::endl;
std::cout << sizeof(c) << std::endl;
std::cout << sizeof(d) << std::endl;
std::cout << sizeof(e) << std::endl;
return 0;
}
Solution for example 1
Figure 5 shows the result of example 1. The "sizeof" is a C++ operator that returns the size of a given argument. It can be used to get not only data types but also classes, structures, and any other user-defined data types.
Example 2 ) Char data type and ASCII codes.
// Example 02
#include <iostream>
int main()
{
char a = 'A'; // English alphabet
char b = 'B'; // English alphabet
char c = '/'; // divide operator
char d = '='; // equal operator
char e = '~'; // tilde character
char f = '바'; // Korean character
char g = 'か'; // Japanese character
std::cout << a << (int)a <<std::endl;
std::cout << b << (int)b << std::endl;
std::cout << c << (int)c <<std::endl;
std::cout << d << (int)d <<std::endl;
std::cout << e << (int)e <<std::endl;
std::cout << f << (int)f <<std::endl;
std::cout << g << (int)g << std::endl;
return 0;
}
You can see that various characters are declared in this example not only English but also Korean and Japanese. The output is the variables themselves and the integer values of them. "(int)" is called type-casting that converts a given type to another data type.
Therefore it shows their ASCII assigned numbers. It works perfectly for English letters and arithmetic characters such as A, B, /, =, but it does not work for Japanese and Korean letters. This is simply because ASCII was made in the USA. I will cover how to express non-English letters later.
Example 3 ) Range of values
// Example 03
#include <iostream>
int main()
{
int a = 2147483647; // Maximum integer
int b = 2147483648; // Maximum integer + 1
int c = 2147483649; // Maximum integer + 2
std::cout << "integer a = " << a << std::endl;
std::cout << "integer b = " << b << std::endl;
std::cout << "integer c = " << c << std::endl;
float af = 1.0/3.0;
double bd = 1.0/3.0;
std::cout.precision(15); //extend the digits of precision of cout function.
std::cout << "float a = " << af << std::endl;
std::cout << "double b = " << bd << std::endl;
return 0;
}
As we already know, the positive maximum of int is 2147483647. What happens if we put 2147483647+1 or 2147483647+2 ?. The answer is simple. The code returns the wrong result.
When it comes to float and double types, the range of values is very important. As explained already, float is a single-point precision data type and double is a double-precision data type. Therefore, double can express precisely twice as long as float. As seen in Figure 3, float a and double b show completely different results from the 7th decimal point.
REFERENCES
(1) Microsoft Visual Studio 2019 Tutorials, https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=vs-2019
'Programming > C++ Basic' 카테고리의 다른 글
[Basic C++] 06 - C++ Operators (0) | 2020.05.07 |
---|---|
[Basic C++] 05 - C++ Functions (0) | 2020.05.06 |
[Basic C++] 04 - Namespace in C++ (0) | 2020.05.06 |
[Basic C++] 03 - How is C++ code formed : Code Structure, Compiling and Linking (0) | 2020.05.06 |
[Basic C++] 01 - How to get MS Visual Studio 2019 and create projects (0) | 2020.05.03 |