|
| 1 | +#include "PrintHelper.h" |
| 2 | + |
| 3 | +/* ************************************* |
| 4 | + * * Fundamental types and expressions * |
| 5 | + * ************************************* |
| 6 | + * |
| 7 | + * Tasks: |
| 8 | + * - Compile the program and analyse the output of the different expressions |
| 9 | + * - Discuss with other students or your tutor in case the result of an expression is a surprise |
| 10 | + * - Fix the marked expressions by changing types such that they produce meaningful results |
| 11 | + * - Answer the questions in the code |
| 12 | + */ |
| 13 | + |
| 14 | +int main() { |
| 15 | + std::cout << "Using literals of different number types:\n"; |
| 16 | + print(5); |
| 17 | + print(5/2.); //FIXME |
| 18 | + print(100/2ull); |
| 19 | + print(2 + 4ull); |
| 20 | + print(2.f + 4ull); |
| 21 | + print(0 - 1 ); // FIXME |
| 22 | + print(1.0000000001 ); // FIXME Why is this number not represented correctly? |
| 23 | + print(1.l+ 1.E-18); // FIXME |
| 24 | + |
| 25 | + std::cout << "\nUsing increment and decrement operators:\n"; |
| 26 | + int a = 1; |
| 27 | + int b; |
| 28 | + int c; |
| 29 | + print(b = a++); // Q: What is the difference between a++ and ++a? |
| 30 | + print(c = ++a); // A: Whether it returns the previous or new value |
| 31 | + print(a); |
| 32 | + print(b); |
| 33 | + print(c); |
| 34 | + |
| 35 | + std::cout << "\nCompound assignment operators:\n"; |
| 36 | + float n = 1; |
| 37 | + print(n *= 2); // Q: Is there a difference between this and the next line? |
| 38 | + print(n *= 2.9); // A: Yes, the computation runs in float and is converted back to int |
| 39 | + print(n -= 1.1f); |
| 40 | + print(n /= 4); // Q: Based on the results of these expressions, is there a better type to be used for n? |
| 41 | + // A: Probably yes, for example float |
| 42 | + |
| 43 | + std::cout << "\nLogic expressions:\n"; |
| 44 | + const bool alwaysTrue = true; |
| 45 | + bool condition1 = false; |
| 46 | + bool condition2 = true; |
| 47 | + print( alwaysTrue && condition1 && condition2 ); |
| 48 | + print( alwaysTrue || condition1 && condition2 ); // Q: Why does operator precedence render this expression useless? |
| 49 | + print( alwaysTrue && condition1 || condition2 ); // A: "true || " is evaluated last. The expression therefore is always true. |
| 50 | + print(condition1 != condition1); // Q: What is the difference between this and the following expression? |
| 51 | + print(condition2 = !condition2); // A: The first is a comparison, the second a negation with subsequent assignment |
| 52 | + print( alwaysTrue && condition1 && condition2 ); |
| 53 | + print( alwaysTrue || condition1 && condition2 ); |
| 54 | + print( alwaysTrue && condition1 || condition2 ); |
| 55 | + |
| 56 | + std::cout << '\n'; |
| 57 | + print( false || 0b10 ); // Q: What is the difference between || and | ? |
| 58 | + print( false | 0b10 ); // A: a boolean operation vs. a bit-wise boolean operation |
| 59 | + printBinary( 0b1 & 0b10 ); |
| 60 | + printBinary( 0b1 | 0b10 ); |
| 61 | + printBinary( 0b1 && 0b10 ); // Q: Are the operators && and || appropriate for integer types? |
| 62 | + printBinary( 0b1 || 0b10 ); // A: Most likely not, because the integers are first converted to boolean |
| 63 | + |
| 64 | + std::cout << "\nPlay with characters and strings:\n"; |
| 65 | + print("a"); // Q: Why is this expression two bytes at run time, the next only one? |
| 66 | + print('a'); // A: Because the first one is a string, which is 0-terminated |
| 67 | + |
| 68 | + char charArray[20]; |
| 69 | + // There are many ways to solve this, for example to use std::string and not manually manage the memory. |
| 70 | + // However, if one really desires to manage a char array, one should at least initialise it with the 0 byte: |
| 71 | + std::fill(std::begin(charArray), std::end(charArray), '\0'); |
| 72 | + char* charPtr = charArray; |
| 73 | + |
| 74 | + print(charArray); |
| 75 | + print(charArray[0] = 'a'); |
| 76 | + print(charArray); |
| 77 | + print(charArray[1] = 98); |
| 78 | + print(charArray); |
| 79 | + print(charPtr); |
| 80 | + // FIXME: Ensure that no unexpected garbage is printed above |
| 81 | +} |
0 commit comments