Skip to content

Commit 78fc0d4

Browse files
committed
fix(data-types): fix data types descriptions
1 parent 21ae5be commit 78fc0d4

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

courses/intro/03-datatypes/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ int bina = 0b101010;
238238
```
239239

240240
Suffixes:
241+
241242
- `no suffix` provided: it will use the first smallest signed integer container that can hold the data starting from `int`;
242243
- `u` or `U`: it will use the first smallest unsigned integer container that can hold the data starting from `unsigned int`;
243244
- `l` or `L`: it will use the first smallest signed integer container that can hold the data starting from `long`;
@@ -254,11 +255,13 @@ unsigned long long l1 = 15731685574866854135ull;
254255
## Float point literals
255256

256257
There are 3 suffixes in floating point decimals.
258+
257259
- `no suffix` means the container is a double;
258260
- `f` suffix means it is a float container;
259261
- `l` suffix means it is a long double container;
260262

261263
A floating point literal can be defined by 3 ways:
264+
262265
- digit-sequence decimal-exponent suffix(optional).
263266
- `1e2` means its a `double` with the value of `1*10^2` or `100`;
264267
- `1e-2f` means its a `float` with the value of `1*10^-2` or `0.01`;
@@ -284,17 +287,20 @@ In C++, you can perform common arithmetic operations is statements using the fol
284287
- Modulus (remainder): `%`
285288

286289
There are two special cases called unary increment / decrement operators that may occur in before(prefixed) or after(postfixed) the variable name [reference](https://en.cppreference.com/w/cpp/language/operator_incdec). If prefixed it is executed first and then return the result, if postfixed, it returns the current value and then execute the operation:
290+
287291
- Increment: `++`;
288292
- Decrement: `--`;
289293

290294
There are shorthand assignment operators [reference](https://en.cppreference.com/w/cpp/language/operator_assignment) that reassign the value of the variable after executing the arithmetic operation with the right side of the operator with the old value of the variable:
295+
291296
- Addition: `+=`
292297
- Subtraction: `-=`
293298
- Multiplication: `*=`
294299
- Division: `/=`
295300
- Modulus (remainder): `%=`
296301

297302
Here is an example of how to use these operators in a C++ program:
303+
298304
```c++
299305
#include <iostream>
300306

@@ -337,6 +343,7 @@ Also, the modulus operator (`%`) returns the remainder of an integer division. F
337343
Implicit casting, also known as type coercion, is the process of converting a value of one data type to another data type without the need for an explicit cast operator. In C++, this can occur when an expression involves operands of different data types and the compiler automatically converts one of the operands to the data type of the other in order to perform the operation.
338344

339345
For example:
346+
340347
```c++
341348
int a = 1;
342349
double b = 1.5;
@@ -346,10 +353,12 @@ int c = a + b; // c is automatically converted to a double before the addition
346353
In this example, the value of `b` is a double, while the value of `a` is an `int`. When the addition operator is used, the compiler will automatically convert a to a `double` before performing the addition. The result of the expression is a `double`, so `c` is also automatically converted to a `double` before being assigned the result of the expression.
347354

348355
Implicit casting can also occur when assigning a value to a variable of a different data type. For example:
356+
349357
```c++
350358
int a = 2;
351359
double b = a; // a is automatically converted to a double before the assignment
352360
```
361+
353362
In this case, the value of `a` is an int, but it is being assigned to a double variable. The compiler will automatically convert the value of `a` to a `double` before making the assignment.
354363

355364
It's important to be aware of implicit casting, because it can sometimes lead to unexpected results or loss of precision if not handled properly. In some cases, it may be necessary to use an explicit cast operator to explicitly convert a value to a specific data type.
@@ -414,6 +423,7 @@ int main() {
414423
return 0;
415424
}
416425
```
426+
417427
You can also use the `std::setw` manipulator to set the minimum field width for the output, which can be useful for aligning the decimal points in a table of numbers.
418428

419429
For example:
@@ -433,11 +443,13 @@ int main() {
433443
return 0;
434444
}
435445
```
446+
436447
Note that these manipulators only affect the output stream, and do not modify the values of the floating point variables themselves. If you want to store the numbers with a fixed precision, you will need to use a different method such as rounding or truncating the numbers.
437448

438449
To align text to the right or left in C++, you can use the `setw` manipulator in the `iomanip` header and the `right` or `left` flag. [More details here](https://en.cppreference.com/w/cpp/io/manip/left)
439450

440451
Here is an example:
452+
441453
```c++
442454
#include <iostream>
443455
#include <iomanip>

0 commit comments

Comments
 (0)