hsf-training / cpluspluscourse

C++ Course Taught at CERN, from Sebastien Ponce (LHCb)
Apache License 2.0
169 stars 65 forks source link

Add an exercise on basic types for the first day #518

Closed hageboeck closed 6 months ago

hageboeck commented 6 months ago

This is an attempt at deepening the understanding of type promotions and operators for basic types. This should address most of the ideas in issue https://github.com/hsf-training/cpluspluscourse/issues/157.

Fix #157

A typical run of this exercise looks like this:

Using literals of different number types:
Line 17: 5                     type=int                 value=5
Line 18: 5/2                   type=int                 value=2
Line 19: 100/2ull              type=unsigned long long  value=50
Line 20: 2 + 4ull              type=unsigned long long  value=6
Line 21: 2.f + 4ull            type=float               value=6
Line 22: 0u - 1u               type=unsigned int        value=4294967295
Line 23: 1.0000000001f         type=float               value=1
Line 24: 1. + 1.E-18           type=double              value=1

Using increment and decrement operators:
Line 30: b = a++               type=int                 value=1
Line 31: c = ++a               type=int                 value=3
Line 32: a                     type=int                 value=3
Line 33: b                     type=int                 value=1
Line 34: c                     type=int                 value=3

Compound assignment operators:
Line 38: n *= 2                type=int                 value=2
Line 39: n *= 2.9              type=int                 value=5
Line 40: n -= 1.1f             type=int                 value=3
Line 41: n /= 4                type=int                 value=0

Logic expressions:
Line 47: alwaysTrue && condition1 && condition2 type=bool                value=0
Line 48: alwaysTrue || condition1 && condition2 type=bool                value=1
Line 49: alwaysTrue && condition1 || condition2 type=bool                value=1
Line 50: condition1 != condition1 type=bool                value=0
Line 51: condition2 = !condition2 type=bool                value=0
Line 52: alwaysTrue && condition1 && condition2 type=bool                value=0
Line 53: alwaysTrue || condition1 && condition2 type=bool                value=1
Line 54: alwaysTrue && condition1 || condition2 type=bool                value=0

Line 57: false || 0b10         type=bool                value=1
Line 58: false | 0b10          type=int                 value=2
Line 59: 0b1 & 0b10            type=int                 value=0000000000000000
Line 60: 0b1 | 0b10            type=int                 value=0000000000000011
Line 61: 0b1 && 0b10           type=bool                value=0000000000000001
Line 62: 0b1 || 0b10           type=bool                value=0000000000000001

Play with characters and strings:
Line 65: "a"                   type=char [2]            value=a
Line 66: 'a'                   type=char                value=a
Line 72: charArray             type=char [20]           value=@U5??
Line 73: charArray[0] = 'a'    type=char                value=a
Line 74: charArray             type=char [20]           value=aU5??
Line 75: charArray[1] = 98     type=char                value=b
Line 76: charArray             type=char [20]           value=ab5??
Line 77: charPtr               type=char*               value=ab5??