[C++] Datatypes

binh12A3
3 min readOct 25, 2021

--

1.0 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 ? 🤔🤔

Let’s take an overview of fundamental datatypes

NOTE :
char is a special type, it’s both character and integer.
The “Minimum Size” depends on compiler and computer architecture.

1. Boolean

2. Character

  • char has size 1 byte (8 bits) and can be used to store the ASCII character (i.e : a, b, c, …, 1, 2, 3, …).
  • char is a special type, we can cast it to both character and integer by using static_cast
  • Code 0 → 31, 127 : control character (non-printable)
  • Escape sequences : are special characters

3. Integer (…-3, -2, -1, 0, 1, 2, 3,…)

  • We can use “signed” and “unsigned” keywords to explicitly declare.
  • The unsigned integer can’t store the negative value, but it can store the posistive value with double range compared with signed integer.

4. Floating point (0.001, 12.9, 3.14,…)

  • Floating point is real number.
  • Floating point doesn’t have unsigned keyword.

NOTE : in some development enviroment, it uniforms long double with double, so the long double is rarely used.

  • We can use “Scientific e notation” to store very big/small number i.e : 3.141592653589793238….You can use “e” or “E” to substitute for 10
  • Precision : Computer memory and data size are limited. With infinite numbers i.e : 3.141592653589793238…., it can store only 3.14159 (by default 6 digits in C++ std::cout). The remaing parts of the number will be truncated and rounded up to 1 unit if the truncated digit > 5.
  • Double is more accurate than float

NOTE :
- Precision affects not only the decimal part but also the integer part
- Float has a low accuracy, so we should use double

  • Rounding issues : We can’t and should not compare “=” or “!=” 2 floating point numbers. We should calculate the difference between 2 flaoting point called “EPSILON”, then if epsilon is really small, then we consider they’re equal.

5. String

  • string is not a built-in datatypes. It’s installed in STL library (C++ Standard Template Library). To use it, we need to “#include <string>” belongs to namespace std.
  • By default cin will get characters until reach “space”, so we should use getline()
  • What we type in console will be stored in cache. For example, you type 26 then press “enter”. Then 26 + “\n” will be stored in cache. So when getline() is called, it’ll read and see “\n” then it’ll stop unexpectedly.In this case we could resolve by “cin.ignore(32767, ‘\n’)”.

The cache can store maximum 32767 characters

  • In some case, we want to input a paragraph, and we don’t want to be break when pressing “enter”. Then we could resolve by using getline() with a 3rd parameter.
  • We can use “+” to connect strings

6. Struct

  • With a new defined struct, we don’t have operators like : +, -, *, /,…In order to use it, we need to define by using “operator*”.

--

--

binh12A3
binh12A3

Written by binh12A3

share to be shared 😎 Made in 🇻🇳

No responses yet