11# Data Types
22
3- ``` admonish warning
4- 🚧 Page Under Construction! 🏗️
5- ```
6-
73As we mentioned on the last page, C++ is a * statically typed* language which means the
84type of data must be known (or deducable) to the compiler. C++ has a large selection of
95types available to use, some are language primitives and others are defined in the
@@ -22,9 +18,9 @@ diffenent bit widths. The default `int` is 32-bits wide on most platforms. By de
2218integer types are signed ie. they can represent both positive and negative numbers. If
2319you need unsigned numbers we can use the ` unsigned ` qualifier.
2420
25- ``` cpp
26- int const x = -5 ;
27- unsigned int const y = 5 ;
21+ ``` cpp,icon=%cplusplus
22+ const int x = -5;
23+ const unsigned int y = 5;
2824```
2925
3026If you need integers of a different sizes you can either use size qualifiers with the
@@ -110,18 +106,18 @@ with the `u` suffix being able to be used in combination with the other two.
110106Additionally you can write integer literals in a different base form by changing the
111107prefix of the literal.
112108
113- ``` cpp
114- auto const decimal = 42 ;
115- auto const octal = 052 ;
116- auto const hex = 0x2a ;
117- auto const Hex = 0X2A ; // capital hex digits
118- auto const binary 0b101010 ;
109+ ``` cpp,icon=%cplusplus
110+ const auto decimal = 42;
111+ const auto octal = 052;
112+ const auto hex = 0x2a;
113+ const auto Hex = 0X2A; // capital hex digits
114+ const auto binary 0b101010;
119115```
120116
121117Integers can also be separated using a ` ' ` to make large numbers easier to read.
122118
123- ``` cpp
124- auto const x = 1'234'567'890 ;
119+ ``` cpp,icon=%cplusplus
120+ const auto x = 1'234'567'890;
125121```
126122
127123### Character Types
@@ -131,17 +127,17 @@ because character types in C++ are represented using numbers, specifically `char
131127represents ASCII code points. Character literals are specified with single quotes like
132128the example below.
133129
134- ``` cpp
135- char const x = ' a' ;
136- auto const y = ' b' ;
130+ ``` cpp,icon=%cplusplus
131+ const char x = 'a';
132+ const auto y = 'b';
137133```
138134
139135### Boolean Type
140136
141137C++'s Boolean type is called ` bool ` and can either hold the value ` true ` or ` false ` .
142138Booleans are used mostly in conditional and loop statements eg. ` if ` and ` while ` .
143139
144- ``` cpp
140+ ``` cpp,icon=%cplusplus
145141bool x = false;
146142auto y = true;
147143```
@@ -164,41 +160,42 @@ floating point number.
164160With ` auto ` , floating point values being initialized as a ` double ` by default and ` float `
165161and ` long double ` literals being specified by ` f ` and ` l ` literal suffixes.
166162
167- ``` cpp
168- auto const f = -0 .06f ;
169- auto const d = 47.5768 ;
170- auto const l = -655456 .457567l ;
163+ ``` cpp,icon=%cplusplus
164+ const auto f = -0.06f;
165+ const auto d = 47.5768;
166+ const auto l = -655456.457567l;
171167```
172168
173169We can also initialize floating points using exponential form:
174170
175- ``` cpp
176- auto const f = -6e-2f ;
177- auto const d = 475768e4 ;
178- auto const l = -655456457567le7l;
171+ ``` cpp,icon=%cplusplus
172+ const auto f = -6e-2f;
173+ const auto d = 475768e4;
174+ const auto l = -655456457567le7l;
179175```
180176
181177#### Arithmetic Operations
182178
183179Integral and floating point types are categorized as * arithmetic* types which mean they
184180support the common arithmetic operations like addition, subtraction etc.
185- ``` cpp
181+
182+ ``` cpp,icon=%cplusplus,fp=main.cxx
186183auto main () -> int {
187184 // addition
188- auto const sum = 4 + 6;
185+ const auto sum = 4 + 6;
189186
190187 // subtraction
191- auto const diff = 10 - 5.5;
188+ const auto diff = 10 - 5.5;
192189
193190 // multiplication
194- auto const mul = 5 * 3.2;
191+ const auto mul = 5 * 3.2;
195192
196193 // division
197- auto const idiv = 10 / 3;
198- auto const fdif = 13.5 / 2.4;
194+ const auto idiv = 10 / 3;
195+ const auto fdif = 13.5 / 2.4;
199196
200197 // remainder
201- auto const = 23 % 4;
198+ const auto = 23 % 4;
202199
203200 return 0;
204201}
@@ -223,14 +220,14 @@ restricted to a set of named variants or *enumerators*. These named constants ha
223220underlying integral type. Specifying the underlying type is optional ie. omit the
224221` : type ` in the enum declaration.
225222
226- ``` cpp
223+ ``` cpp,icon=%cplusplus
227224enum class colour : char {
228225 red,
229226 green,
230227 blue
231228};
232229
233- auto const c = colour::red;
230+ const auto c = colour::red;
234231```
235232
236233
@@ -242,33 +239,33 @@ declared. Tuples in C++ are not language types but are provided by the standard
242239in the ` <tuple> ` header and is called ` std::tuple ` . We create a tuple using brace
243240initialization (top) or using the helper function ` std::make_tuple() ` .
244241
245- ```cpp
246- auto const t = std::tuple { 5u, 5.34f, -345, "abc", false };
247- auto const u = std::make_tuple(5u, 5.f, -345, "abc", false);
242+ ``` cpp,icon=%cplusplus
243+ const auto t = std::tuple { 5u, 5.34f, -345, "abc", false };
244+ const auto u = std::make_tuple(5u, 5.f, -345, "abc", false);
248245```
249246
250247Tuples can be accessed using ` std::get<I>(t) ` with ` I ` being the index of the value we
251248want to access and ` t ` is the tuple object.
252249
253- ``` cpp
254- auto const e = std::get<2 >(t); // e := -345
250+ ``` cpp,icon=%cplusplus
251+ const auto e = std::get<2>(t); // e := -345
255252```
256253
257254You can also destructure tuples into its constituent values like so.
258255
259- ``` cpp
260- auto const [v, w, x, y, z] = t;
256+ ``` cpp,icon=%cplusplus
257+ const auto [v, w, x, y, z] = t;
261258```
262259
263260There is a specialization of * tuples* called ` std::pair ` which holds just two values. The
264261values of a pair can be extracted using the same methods as tuples but they also have
265262public members ` std::pair::first ` and ` std::pair::second ` which allows you to access the
266263data.
267264
268- ``` cpp
269- auto const p = std::pair {5, 'a'};
270- auto const [ x, y] = p;
271- auto const z = p.second;
265+ ``` cpp,icon=%cplusplus
266+ const auto p = std::pair {5, 'a'};
267+ const auto [x, y] = p;
268+ const auto z = p.second;
272269```
273270
274271## Special Types
@@ -279,8 +276,8 @@ fundamental to the language.
279276The first is the ` void ` type is an incomplete type that is used to indicate that a
280277function does not return a value.
281278
282- ```cpp
283- auto foo(auto const i) -> void {
279+ ``` cpp,icon=%cplusplus
280+ auto foo(const auto i) -> void {
284281 i + 5;
285282}
286283```
@@ -297,9 +294,9 @@ with indices starting at 0. The subscript element access does not perform bounds
297294while ` array::at() ` does, meaning the later will throw and exception if an out of bounds
298295index is used while the former will crash the program... sometimes.
299296
300- ``` cpp
301- auto const a = std::array { 1, 2, 3, 4, 5 };
302- auto const e1 = a[ 0] ; // valid
303- auto const e2 = a.at(5); // exception std::out_of_range
297+ ``` cpp,icon=%cplusplus
298+ const auto a = std::array { 1, 2, 3, 4, 5 };
299+ const auto e1 = a[0]; // valid
300+ const auto e2 = a.at(5); // exception std::out_of_range
304301```
305302
0 commit comments