You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- `1e2` means its a `double` with the value of `1*10^2` or `100`;
264
267
- `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
284
287
- Modulus (remainder): `%`
285
288
286
289
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
+
287
291
- Increment: `++`;
288
292
- Decrement: `--`;
289
293
290
294
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
+
291
296
- Addition: `+=`
292
297
- Subtraction: `-=`
293
298
- Multiplication: `*=`
294
299
- Division: `/=`
295
300
- Modulus (remainder): `%=`
296
301
297
302
Here is an example of how to use these operators in a C++ program:
303
+
298
304
```c++
299
305
#include<iostream>
300
306
@@ -337,6 +343,7 @@ Also, the modulus operator (`%`) returns the remainder of an integer division. F
337
343
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.
338
344
339
345
For example:
346
+
340
347
```c++
341
348
int a = 1;
342
349
double b = 1.5;
@@ -346,10 +353,12 @@ int c = a + b; // c is automatically converted to a double before the addition
346
353
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.
347
354
348
355
Implicit casting can also occur when assigning a value to a variable of a different data type. For example:
356
+
349
357
```c++
350
358
int a = 2;
351
359
double b = a; // a is automatically converted to a double before the assignment
352
360
```
361
+
353
362
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.
354
363
355
364
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() {
414
423
return 0;
415
424
}
416
425
```
426
+
417
427
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.
418
428
419
429
For example:
@@ -433,11 +443,13 @@ int main() {
433
443
return 0;
434
444
}
435
445
```
446
+
436
447
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.
437
448
438
449
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)
0 commit comments